繁体   English   中英

隐藏单元格中的自定义UITableviewcell更新

[英]Custom UITableviewcell updates in hidden cells

我使用了自定义UITableViewCell。 我在其中添加了手势手势选项,在tableView有20个单元格。 如果我滑动第一个单元格并滚动,则意味着第11个单元格也更新为我的第一个单元格值。

以下是我的代码片段。

    - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    CustomCell *cell = (CustomCell*)
        [tableView dequeueReusableCellWithIdentifier:@"CustomCellId”];
     [cell setRequestCellDelegate:self];
    [cell.swipeLeft addTarget:self action:@selector(swipeLeftAction:)];
    cell.tag = indexPath.row;
    cell.swipeLeft.delegate = self;
    cell.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    cell.indexpath = indexPath;
    [cell.swipeRight addTarget:self action:@selector(swipeRightAction:)];
    cell.swipeRight.delegate = self;
    cell.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    cell.tableHoldButtn.tag = indexPath.row;

    return cell;
}

请帮助找到解决方案。

之所以发生这种情况是因为,一旦您的第一个滑动单元离开了屏幕,它就会被放入队列以供重用。 然后,当第10个单元格出现在屏幕上时,它不是在创建中,而是第一个单元格正在重用。 并且由于您已对其进行了刷卡,因此它将以与离开屏幕时完全相同的状态出队。

您应该在表视图控制器中跟踪应擦除的单元格的更改,并在cellForIndexPath数据源方法中恢复该状态。

我认为最好在使用手势时使用tableView委托方法。 您可以使用

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

此外,您还可以通过以下方式自定义编辑按钮的外观:

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;
CustomCell *cell = (CustomCell*)
        [tableView dequeueReusableCellWithIdentifier:[NSString stringwithFormat:"%ld",indexPath.row]];

重用标识符更改为indexPath.row的字符串

希望我的实验能对您有所帮助

注意:不要在其他地方注册单元格,在其他地方删除注册单元格(也许在viewdidLoad中)

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    CustomCell *cell = (CustomCell*)
//    [tableView dequeueReusableCellWithIdentifier:@"CustomCellId”];
// need not register cell  in other ,like viewdidLoad
    CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@",indexPath.row]];

     if (!cell) {
         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"%@",indexPath.row]];
         cell.selectionStyle =     UITableViewCellSelectionStyleNone;
     }

     [cell setRequestCellDelegate:self];
     [cell.swipeLeft addTarget:self action:@selector(swipeLeftAction:)];
     cell.tag = indexPath.row;
     cell.swipeLeft.delegate = self;
     cell.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
     cell.indexpath = indexPath;
     [cell.swipeRight addTarget:self action:@selector(swipeRightAction:)];
     cell.swipeRight.delegate = self;
     cell.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
     cell.tableHoldButtn.tag = indexPath.row;

     return cell;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM