Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

✔ Checkmark selected row in UITableViewCell

I am an iOS development newbie. I want to add a checkmark to my UITableViewCell when it is selected. The checkmark should be removed when another row is selected. How would I do this?

like image 334
Suchi Avatar asked Nov 02 '11 15:11

Suchi


2 Answers

Do not use [tableview reloadData]; // its a hammer.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath {     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; }  -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } 
like image 191
Ujwal Manjunath Avatar answered Sep 30 '22 14:09

Ujwal Manjunath


In your UITableViewDatasource method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"Cell";     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];      if(cell == nil )     {         cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];     }     if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)      {         cell.accessoryType = UITableViewCellAccessoryCheckmark;     }      else      {         cell.accessoryType = UITableViewCellAccessoryNone;     }     return cell; }  // UITableView Delegate Method -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     self.lastIndexPath = indexPath;      [tableView reloadData]; } 

And lastIndexPath is a property(strong) NSIndexPath* lastIndexPath;

like image 36
0x8badf00d Avatar answered Sep 30 '22 14:09

0x8badf00d