user288231
user288231

Reputation: 993

UITableViewCell weird behavior

Hello I have recently met up with a problem on UITableViewCell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ContentCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    CloseButton *cButton = (CloseButton *)[cell viewWithTag:20];
    [cButton addTarget:self action:@selector(deleteDetector:) forControlEvents:UIControlEventTouchUpInside];
     ...
return cell;
}

Later, on my delete detector:

-(void)deleteDetector:(id)sender {
    CloseButton *cButton = (CloseButton *)sender;

    [cButton setHidden:YES];
}

When I start scrolling down to like 1000 cells, the buttons start to appear and some of them starts to disappear.

Upvotes: 0

Views: 42

Answers (1)

JoeFryer
JoeFryer

Reputation: 2751

Ok, so if I understand your question correctly, I assume what's going on is:

You are pressing the button on a cell, which makes the button hidden. Then, you scroll further down, and another cell appears with the button already hidden (even though you haven't pressed the button for that row yet).

This is because your cells are actually being reused, meaning that when one of the cells that has already has the button hidden gets reused, that button will still be hidden (as it is actually the same cell). The 'quick fix' to prove this is to unhide the button in your tableView:cellForRowAtIndexPath: method, like so:

[cButton setHidden:NO];

Do this somewhere after this, obviously:

CloseButton *cButton = (CloseButton *)[cell viewWithTag:20];

This should prevent cells appearing with the button hidden when they shouldn't. However, it will also mean that if you press the button on a cell, and it then goes off screen and comes back on, it will also then show the button again, when you probably don't want it to. You'll have to keep a track of which rows you have pressed the button on in your model somewhere if you don't want that to happen.

Upvotes: 1

Related Questions