I want to implement multiple row selection in UITableview.but when i select multiple row with checkmark and scroll down,the checkmarks are disappears when i come back to selected rows section.I have tried many solutions from Stackoverflow but it doesn't work for me.Can you please anyone give me the solution which works for me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if([_selectedstatearray containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
NSLog(@"cellText>>%@",cellText);
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[_selectedstatearray removeObject:cellText];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[_selectedstatearray addObject:cellText];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
NSLog(@"selectedarray>>%@",_selectedstatearray);
NSString *greeting = [_selectedstatearray componentsJoinedByString:@","];
NSLog(@"%@",greeting);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}