DevCali
DevCali

Reputation: 398

Can't hide the custom checkmark in the didSelectRowAtIndexPath

I tried to put a custom UIButton with an image on a custom tableview cell to use it as checkmark, 1st tap, hides the checkmark and next tap brings it back... i'm trying the below code "example code" to perform this function but its not hiding the checkmark i.e."custom UIButton". not sure what am i missing here?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{
   NSLog(@"%@",indexPath);

    TableViewCell *customCell = [[TableViewCell alloc]init];
    if (customCell.checkMarkButton.hidden == NO) {
    customCell.checkMarkButton.hidden = YES;
    } else {
    customCell.checkMarkButton.hidden = NO;
    }
}

Upvotes: 0

Views: 183

Answers (2)

oiledCode
oiledCode

Reputation: 8649

The problem is that in didSelectRowAtIndexPath you alloc a new cell.

Try this :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath {
      TableViewCell *customCell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
      customCell.checkMarkButton.hidden = !customCell.checkMarkButton.hidden;
}

Upvotes: 1

Levi
Levi

Reputation: 7343

Replace this line: TableViewCell *customCell = [[TableViewCell alloc]init]; with this: TableViewCell *customCell = [tableView cellForRowAtIndexPah:indexPath];. You don't have to crate a new cell, just get the one that was tapped.

Upvotes: 2

Related Questions