gpt4 book ai didi

ios - 如何删除 TableView ios 的索引路径中的重复项,

转载 作者:行者123 更新时间:2023-11-29 01:23:10 25 4
gpt4 key购买 nike

当我增加第一个项目的数量时,第七个项目的数量增加....每次都会发生这种情况 因为 TableView cellforrowatindexPath 在滚动时重新加载。我找不到解决方案。我该如何解决这个问题。我搜索了很多但找不到任何解决方案请帮忙。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

static NSString *simpleTableIdentifier = @"MenuSelectCell";
ItemSelectTableViewCell *cell = (ItemSelectTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemSelectTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];


if ([[[self.restaurantModal.dataArray objectAtIndex:indexPath.row] objectForKey:@"data" ] count] >0) {
[cell.selectFlavorBtn addTarget:self action:@selector(selectFlavorMethod:) forControlEvents:UIControlEventTouchUpInside];

[cell.addToCartBtn addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];

[cell.fullQtyPlussBtn addTarget:self action:@selector(updateQuantity:) forControlEvents:UIControlEventTouchUpInside];

[cell.fullQtyMinusBtn addTarget:self action:@selector(updateQuantity:) forControlEvents:UIControlEventTouchUpInside];

}

最佳答案

滚动后,tableView 将不会创建其他单元格。它将重用已创建的单元格。

If a cell object is reusable—the typical case—you assign it a reuse identifier (an arbitrary string) in the storyboard. At runtime, the table view stores cell objects in an internal queue. When the table view asks the data source to configure a cell object for display, the data source can access the queued object by sending a dequeueReusableCellWithIdentifier: message to the table view, passing in a reuse identifier. The data source sets the content of the cell and any special properties before returning it. This reuse of cell objects is a performance enhancement because it eliminates the overhead of cell creation.

更多详情here .所以你的代码应该是这样的:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
static NSString *simpleTableIdentifier = @"MenuSelectCell";

ItemSelectTableViewCell *cell = (ItemSelectTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemSelectTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}


if ([[[self.restaurantModal.dataArray objectAtIndex:indexPath.row] objectForKey:@"data" ] count] >0) {
[cell.selectFlavorBtn addTarget:self action:@selector(selectFlavorMethod:) forControlEvents:UIControlEventTouchUpInside];

[cell.addToCartBtn addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];

[cell.fullQtyPlussBtn addTarget:self action:@selector(updateQuantity:) forControlEvents:UIControlEventTouchUpInside];

[cell.fullQtyMinusBtn addTarget:self action:@selector(updateQuantity:) forControlEvents:UIControlEventTouchUpInside];
}
}

关于ios - 如何删除 TableView ios 的索引路径中的重复项,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com