Grace
Grace

Reputation: 91

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.

Upvotes: 0

Views: 657

Answers (4)

Ujwal Manjunath
Ujwal Manjunath

Reputation: 2970

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

Upvotes: 1

Mudit Bajpai
Mudit Bajpai

Reputation: 3020

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

Upvotes: 0

Musthafa P P
Musthafa P P

Reputation: 655

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;

    }
.....................
.....................
.....................
}

Upvotes: 0

Vidya Murthy
Vidya Murthy

Reputation: 530

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.

Upvotes: 0

Related Questions