Reputation: 556
The following code starts with a blank cell and add a checkmark when I select the cell but, I want to remove all checkmarks while I am tapping button externally. Can you please suggest me how can I implement this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
labelInfo.text=@"1";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=@"2";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=@"3";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=@"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=@"5";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
}
}
Upvotes: 0
Views: 390
Reputation: 657
Try the below code., Just update the previous clicked index row by using a flag value. hope it may helps.
@interface ViewController ()
{
int previousClickedIndex; //To save previous clicked row
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
previousClickedIndex = -1; //Initialize with a negative value
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
if(previousClickedIndex>-1)
{
UITableViewCell *prevousSelectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:previousClickedIndex inSection:indexPath.section]];
prevousSelectedCell.accessoryType = UITableViewCellAccessoryNone;
}
switch (indexPath.row) {
case 0:
//labelInfo.text=@"1";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
//labelInfo.text=@"2";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
//labelInfo.text=@"3";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
//labelInfo.text=@"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
//labelInfo.text=@"5";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
previousClickedIndex = indexPath.row;
}
}
Upvotes: 0
Reputation: 3008
if you are new the easiest solution would be to manage NSMutableArray for selected index.
NSMutableArray * selectedArray = [NSMutableArray new];
On every Did select Check if selected indexpath is already present in your array?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([selectedArray containsObject:indexPath]){
//Your cell is already selected then unselect it
[selectedArray removeObject:indexPath];
}
else{
//Add object to indexpath
[selectedArray addObject:indexPath];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
This is your cellForRowAtIndexPath will be
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"YOUR_ID" forIndexPath:indexPath];
[cell setAccessoryType: UITableViewCellAccessoryNone];
if ([selectedArray containsObject:indexPath]){
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
}
return cell;
}
When You want to clear all check mark remove all item from array and reload cell or
[YOUR_TABLEVIEW reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
Upvotes: 0
Reputation: 4356
In button's IBAction
reload the table and in cellForRowAtIndexPath
write cell.accessoryType = UITableViewCellAccessoryNone;
.
Upvotes: 1