Search code examples
objective-ciosuitableviewcheckmark

UITableViewCellAccessoryCheckmark in different sections


I have 2 sections in a UITableView. I would like to enable the UITableViewCellAccessoryCheckmark on the row I have selected.
There can only be 1 checked row in the 2 sections.
How can I do that? The examples I found only shows how to do it with 1 section.

Thank you.


Solution

  • Here i am assuming total number of rows in each section is 3 and total number of section is 2.

     - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    
           for(NSInteger i=0;i<2;i++) 
           {
    
    
            for (int index=0; index<3; index++) 
            {
                NSIndexPath *indexpath=[NSIndexPath indexPathForRow:index inSection:i];
                UITableViewCell *cell=[tableView1 cellForRowAtIndexPath:indexpath];
    
                if ([indexPath compare:indexpath] == NSOrderedSame) 
                {
    
                    cell.accessoryType=UITableViewCellAccessoryCheckmark;
                }
                else
                {
    
                    cell.accessoryType=UITableViewCellAccessoryNone;
                }
            }
           }
    }