0

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.

Grace
  • 91
  • 3
  • 11

4 Answers4

1

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;
        }
    }
}
Ujwal Manjunath
  • 2,970
  • 2
  • 15
  • 3
0

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.

Vidya Murthy
  • 530
  • 1
  • 8
  • 22
0

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;

    }
.....................
.....................
.....................
}
Musthafa P P
  • 645
  • 3
  • 7
  • 21
0

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;
            }
        }
       }
}
Mudit Bajpai
  • 3,010
  • 19
  • 34
  • Thanks for the reply(: the codes above allows a checkmark in each section however I want only 1 checkmark in the sections. – Grace Feb 16 '12 at 05:17
  • hi i have updated my answer, previously i thought you want one check mark for each section. now it's according to your requirement means only one check mark at on time among all section. – Mudit Bajpai Feb 16 '12 at 05:49
  • hi thanks(: it works. but what if i have different number of rows in each section. will it still work? – Grace Feb 16 '12 at 07:07
  • yes it works, but make sure that currently in inner loop is executing 3 times, bcoz total number of rows in each section is 3, which is maximum. if you want different number of rows in each section, then no problem with this code you only replace 3(number of rows currently)to that number of rows which is maximum among all section,try this. – Mudit Bajpai Feb 16 '12 at 07:48