Using UITableViewCellAccessoryCheckMark

I am trying to make this work, but I seem to be unable to ponder the idea.

So far I have this where you can select a cell and it will be able to create a checkmark. Then, when you select another cell, the previous checkmark will disappear, and tableview will make a new checkmark for the selected new cell. All of this works great.

However, I want to do this when, when you select the same cell with a checkmark, the checkmark does not disappear. But it is so!

I tried many if posts to find out if I can figure out how to do this, but cannot find a solution. Perhaps there is something insignificant that I need to change in my code.

Here is my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(self.checkedIndexPath) { UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath]; uncheckCell.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } if([self.checkedIndexPath isEqual:indexPath]) { self.checkedIndexPath = nil; } else { cellCheck = [tableView cellForRowAtIndexPath:indexPath]; cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; self.checkedIndexPath = indexPath; [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSLog(@"%@", indexPath); } } 
+4
source share
2 answers

If you select the check box, this code will be executed.

 if(self.checkedIndexPath) { UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath]; uncheckCell.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

This code will remove the checkmark that you added during the first selection.

then this code will be executed

 if([self.checkedIndexPath isEqual:indexPath]) { self.checkedIndexPath = nil; } 

Thus, only when you select the same cell again, a checkmark will appear again

I think a cleaner way would be like this.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath]; cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:indexPath]; uncheckCell.accessoryType = UITableViewCellAccessoryNone; } 

AS you can get the value for self.checkedIndexPath from [tableView indexPathForSelectedRow]; setting self.checkedIndexPath is optional depending on the logic of your code.

+12
source

You don’t manipulate cells directly like this, save the checked state somewhere and change the membership attribute in cellForRowAtIndex:

 static BOOL checkedRows[100]; // example data storage - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; { checkedRows[indexPath.row] = !checkedRows[indexPath.row]; [tableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"somecell"]; if(!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.accessoryType = checkedRows[indexPath.row] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; return cell; } 
0
source

Source: https://habr.com/ru/post/1443881/


All Articles