简体   繁体   中英

Custom UITableviewcell updates in hidden cells

I have used custom UITableViewCell. I have added a Gesture in it for swipe option, I have 20 cells in my tableView . If I swipe my first cell and scroll means my 11th cell also updated into my first cell value.

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. 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.

I think its better to use tableView delegate methods instead on applying gesture. 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

I hope my experiment can help you

note: do not register cell in other place,remove register cell in other place (maybe in 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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