【问题标题】:How to remove duplicacy in index path of table view ios,如何删除tableview ios的indexpath中的重复项,
【发布时间】:2015-12-18 11:12:47
【问题描述】:

当我增加第 1 项的数量时,第 7 项的数量会增加....这种情况每次都会发生 因为表格视图 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];

    }

【问题讨论】:

    标签: ios iphone uitableview custom-cell


    【解决方案1】:

    滚动后,tableView 不会创建另一个单元格。它将重用已创建的单元格。

    如果一个单元格对象是可重用的——典型的情况——你给它分配一个重用 故事板中的标识符(任意字符串)。在运行时, 表视图将单元对象存储在内部队列中。当表 view 要求数据源配置一个单元格对象进行显示, 数据源可以通过发送一个 dequeueReusableCellWithIdentifier:向表视图发送消息,传递 在重用标识符中。数据源设置单元格的内容 和返回之前的任何特殊属性。这种细胞的重用 对象是一种性能增强,因为它消除了 单元创建的开销。

    更多详情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];
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为这背后的原因是您的 dequed 单元格仍然具有先前索引中的参数。您可以在重用单元之前重置,也可以实现 prepareForReuse 委托方法。

      【讨论】:

        【解决方案3】:

        试试 ItemSelectTableViewCell *cell = (ItemSelectTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-17
          • 1970-01-01
          • 2016-11-09
          • 1970-01-01
          • 2016-01-21
          • 2016-12-25
          • 1970-01-01
          • 2012-08-10
          相关资源
          最近更新 更多