iphone - Set one check mark for each section in UITableView and save its state -


spent hours , hours , no use.i don't understand whether not effective in searching(googling) or there less questions on or might have committed mistake while implementing answers of experts!

i know there several questions on setting accessory type check mark 1 row , none other rows in section,traced out posts here , there.

i have 2 sections in table view.by default want 1st row in each section selected i.e. accessory view check mark.now here upon user selection of row,i want check mark visible on selected row only.i have tried declaring 2 index paths keep track of row selection in each section.here implementation code:

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {         nsstring *cellidentifier = [nsstring stringwithformat:@"s%1dr%1d",indexpath.row,indexpath.section];     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];      if (indexpath.section == 0)     {         if(self.firstsectionindex == indexpath)         {             cell.accessorytype = uitableviewcellaccessorynone;         }         else         {             cell.accessorytype = uitableviewcellaccessorycheckmark;         }     }      if (indexpath.section == 1)     {         if(self.secondsectionindex == indexpath)         {             cell.accessorytype = uitableviewcellaccessorynone;         }         else         {             cell.accessorytype = uitableviewcellaccessorycheckmark;         }     }      if (cell == nil)     {         cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];         cell.backgroundcolor = [uicolor clearcolor];         cell.textlabel.textcolor = [uicolor whitecolor];          switch (indexpath.section)         {             case 0:             {                 if (indexpath.row == 0)                 {                     if(self.firstsectionindex != indexpath)                     {                         cell.accessorytype = uitableviewcellaccessorycheckmark;                     }                     else                     {                         cell.accessorytype = uitableviewcellaccessorynone;                     }                     cell.textlabel.text = @"yes";                 }                 if (indexpath.row == 1)                 {                     cell.textlabel.text = @"no";                 }             }                 break;             case 1:             {                 if (indexpath.row == 0)                 {                     if(self.secondsectionindex != indexpath)                     {                         cell.accessorytype = uitableviewcellaccessorycheckmark;                     }                     else                     {                         cell.accessorytype = uitableviewcellaccessorynone;                     }                     cell.textlabel.text = @"easy";                 }                 if (indexpath.row == 1)                 {                     cell.textlabel.text = @"medium";                 }                 if (indexpath.row == 2)                 {                     cell.textlabel.text = @"hard";                 }             }                 break;              default:                 break;         }     }      tableview.backgroundcolor = [uicolor clearcolor];     tableview.backgroundview = nil;      return cell; } 

in didselectrowatindexpath,i have done following:

-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {     if (indexpath.section == 0)     {       self.firstsectionindex = indexpath;     }     if (indexpath.section == 1)     {        self.secondsectionindex = indexpath;     }     [tableview reloaddata]; } 

i have searched state of cell selection saved long term reference,in quest of it,i found useful link here.

but selecting multiple cells , accessory type check mark being applied rows in section.i don't understand what's wrong.can 1 please guide me on this!!

thanks in advance :)

you can achieve using bellow implementation of code :-

edited

.h file

nsmutablearray *firstselectedcellsarray; nsmutablearray *secondselectedcellsarray; nsmutablearray *thirdselectedcellsarray; 

in .m file

    -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {     if (indexpath.section == 0)     {         nsnumber *rownumber = [nsnumber numberwithunsignedint:indexpath.row];          uitableviewcell *selectedcell = [tableview cellforrowatindexpath:indexpath];         if (selectedcell.accessorytype == uitableviewcellaccessorynone)         {             selectedcell.accessorytype = uitableviewcellaccessorycheckmark;             if ( [firstselectedcellsarray containsobject:rownumber]  )             {                 [firstselectedcellsarray removeobject:rownumber];             }             else             {                 [firstselectedcellsarray addobject:rownumber];             }         }         else if (selectedcell.accessorytype == uitableviewcellaccessorycheckmark)         {             if ( [firstselectedcellsarray containsobject:rownumber]  )             {                 [firstselectedcellsarray removeobject:rownumber];             }             else             {                 [firstselectedcellsarray addobject:rownumber];             }             selectedcell.accessorytype = uitableviewcellaccessorynone;         }     }      if (indexpath.section == 1)     {         nsnumber *rownumber = [nsnumber numberwithunsignedint:indexpath.row];          uitableviewcell *selectedcell = [tableview cellforrowatindexpath:indexpath];         if (selectedcell.accessorytype == uitableviewcellaccessorynone)         {             selectedcell.accessorytype = uitableviewcellaccessorycheckmark;             if ( [secondselectedcellsarray containsobject:rownumber]  )             {                 [secondselectedcellsarray removeobject:rownumber];             }             else             {                 [secondselectedcellsarray addobject:rownumber];             }         }         else if (selectedcell.accessorytype == uitableviewcellaccessorycheckmark)         {             if ( [secondselectedcellsarray containsobject:rownumber]  )             {                 [secondselectedcellsarray removeobject:rownumber];             }             else             {                 [secondselectedcellsarray addobject:rownumber];             }             selectedcell.accessorytype = uitableviewcellaccessorynone;         }     }     if (indexpath.section == 2)     {         nsnumber *rownumber = [nsnumber numberwithunsignedint:indexpath.row];          uitableviewcell *selectedcell = [tableview cellforrowatindexpath:indexpath];         if (selectedcell.accessorytype == uitableviewcellaccessorynone)         {             selectedcell.accessorytype = uitableviewcellaccessorycheckmark;             if ( [thirdselectedcellsarray containsobject:rownumber]  )             {                 [thirdselectedcellsarray removeobject:rownumber];             }             else             {                 [thirdselectedcellsarray addobject:rownumber];             }         }         else if (selectedcell.accessorytype == uitableviewcellaccessorycheckmark)         {             if ( [thirdselectedcellsarray containsobject:rownumber]  )             {                 [thirdselectedcellsarray removeobject:rownumber];             }             else             {                 [thirdselectedcellsarray addobject:rownumber];             }             selectedcell.accessorytype = uitableviewcellaccessorynone;         }     }  } 

now in cellforrowatindexpath put littel piece of code:-

 - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      static nsstring *cellidentifier = @"cell";      uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];     if (cell == nil) {         cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier] autorelease];     }      if(indexpath.section==0)     {          nsnumber *rownsnum = [nsnumber numberwithunsignedint:indexpath.row];         if ( [firstselectedcellsarray containsobject:rownsnum]  )         {             cell.accessorytype = uitableviewcellaccessorycheckmark;         }         else         {             cell.accessorytype = uitableviewcellaccessorynone;         }     }     if(indexpath.section==1)     {          nsnumber *rownsnum = [nsnumber numberwithunsignedint:indexpath.row];         if ( [secondselectedcellsarray containsobject:rownsnum]  )         {             cell.accessorytype = uitableviewcellaccessorycheckmark;         }         else         {             cell.accessorytype = uitableviewcellaccessorynone;         }     }     if(indexpath.section==2)     {          nsnumber *rownsnum = [nsnumber numberwithunsignedint:indexpath.row];         if ( [thirdselectedcellsarray containsobject:rownsnum]  )         {             cell.accessorytype = uitableviewcellaccessorycheckmark;         }         else         {             cell.accessorytype = uitableviewcellaccessorynone;         }     }      return cell; } 

demo link

http://www.sendspace.com/file/z6oxg2

screenshot:

enter image description here


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -