简体   繁体   English

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

[英]Custom UITableviewcell updates in hidden cells

I have used custom UITableViewCell. 我使用了自定义UITableViewCell。 I have added a Gesture in it for swipe option, I have 20 cells in my tableView . 我在其中添加了手势手势选项,在tableView有20个单元格。 If I swipe my first cell and scroll means my 11th cell also updated into my first cell value. 如果我滑动第一个单元格并滚动,则意味着第11个单元格也更新为我的第一个单元格值。

the following is my code snipet. 以下是我的代码片段。

    - (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;
}

Please help to find the solution for this. 请帮助找到解决方案。

This is happenning because as soon as your first swiped cell goes off the screen it's being put into queue for reuse. 之所以发生这种情况是因为,一旦您的第一个滑动单元离开了屏幕,它就会被放入队列以供重用。 Then when your 10th cell should come on the screen it's not being created but rather 1st cell is being reused. 然后,当第10个单元格出现在屏幕上时,它不是在创建中,而是第一个单元格正在重用。 And since you have swiped it, it will be dequeued in exact same state as it left the screen. 并且由于您已对其进行了刷卡,因此它将以与离开屏幕时完全相同的状态出队。

You should track changes in table view controller which cell should be swiped and restore that state in your cellForIndexPath data source method. 您应该在表视图控制器中跟踪应擦除的单元格的更改,并在cellForIndexPath数据源方法中恢复该状态。

I think its better to use tableView delegate methods instead on applying gesture. 我认为最好在使用手势时使用tableView委托方法。 you can use 您可以使用

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

Additionally you can also customise the edit button appearance by: 此外,您还可以通过以下方式自定义编辑按钮的外观:

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

reuse Identifier change to the string of indexPath.row 重用标识符更改为indexPath.row的字符串

I hope my experiment can help you 希望我的实验能对您有所帮助

note: do not register cell in other place,remove register cell in other place (maybe in viewdidLoad ) 注意:不要在其他地方注册单元格,在其他地方删除注册单元格(也许在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