Augustin A
Augustin A

Reputation: 127

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.

Upvotes: 0

Views: 74

Answers (4)

wg_hjl
wg_hjl

Reputation: 545

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

Upvotes: 0

wg_hjl
wg_hjl

Reputation: 545

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

Upvotes: 0

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

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;

Upvotes: 0

Eimantas
Eimantas

Reputation: 49354

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.

Upvotes: 1

Related Questions