Question

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.

Was it helpful?

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;
            }
        }
       }
}

OTHER TIPS

this will works for any number of section and cell within in each section

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section]].accessoryType = UITableViewCellAccessoryCheckmark;
    [self deSelectOtherCellsInTableView:tableView Except:indexPath]; 
}

-(void)deSelectOtherCellsInTableView:(UITableView *)tableView Except:(NSIndexPath *)indexPath
{
    for(UITableViewCell *cell in [tableView visibleCells]){
        NSIndexPath *index = [tableView indexPathForCell:cell];
        if(index.section == indexPath.section && index.row != indexPath.row){
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

You could keep a variable that stores the section number and the row number, and a bool to keep a track if the selection has been made. In the didSelectRow method, you can access these values to remove the selection from the previous cell, if any, and update them to the current selected row and section values.

This may be you can help.On Delegate Method of UITavleview you can implement this code

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==1)
    {
        NSUInteger row=indexPath.row;

//  implement you code  for first section;

    }
    elseif(indexPath.section==2)
    {
        NSUInteger row=indexPath.row;
//implement you code  second first section;

    }
.....................
.....................
.....................
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top