Checkmarks in a UITableView

I am creating a table with 20 cells.
And when I select the first line, a checkmark will be displayed on it.

enter image description here

But when I look at the table view, then this is not only one checkmark on the tableview cell.
It also appears in another cell.

enter image description here

What is the problem with this?

self.dataAry = [NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",nil]; marks = [NSMutableArray new]; for (int i = 0 ; i < [self.dataAry count]; i++) { [marks addObject:@"NO"]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = [dataAry objectAtIndex:indexPath.row]; if ([[marks objectAtIndex:indexPath.row] isEqualToString:@"YES"]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; }else { [cell setAccessoryType:UITableViewCellAccessoryNone]; } // Configure the cell. return cell;} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark){ [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone]; [selectArray removeObject:[self.dataAry objectAtIndex:indexPath.row]]; [marks replaceObjectAtIndex:indexPath.row withObject:@"NO"]; } else { [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark]; [selectArray addObject:[self.dataAry objectAtIndex:indexPath.row]]; [marks replaceObjectAtIndex:indexPath.row withObject:@"YES"]; } } 
+6
source share
4 answers

I think this is due to the reuse of the label cell. To fix this, write this:

 *if ([[marks objectAtIndex:indexPath.row] isEqualToString:@"YES"]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; }else { [cell setAccessoryType:UITableViewCellAccessoryNone];* 

Out of the cell reuse space. This is after: ...} // Configure the cell.

before

 *return cell;* 
+6
source

Try it. Here selectedRow is an integer. Assign selectedRow = -1 to viewDidLoad.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; int row = [indexPath row]; cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectedRow = [indexPath row]; [tableView reloadData]; } 
+5
source

check this

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; if (thisCell.accessoryType == UITableViewCellAccessoryNone) { thisCell.accessoryType = UITableViewCellAccessoryCheckmark; }else{ thisCell.accessoryType = UITableViewCellAccessoryNone; } } - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath { //add your own code to set the cell accesory type. return UITableViewCellAccessoryNone; } 
+2
source

i ran into the same problem. for me, the solution added one line of code.

 cell.accessoryType = UITableViewCellAccessoryNone; 

adding the above code after:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } 

this solution may not be suitable for the encoder who posted the question, but may help new people like me who are facing the same problem as the original quesiton.

0
source

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


All Articles