iphone - UITableView ticks cells randomly when they leave the frame -


i have uitableview in can tick 4 cells. if tick amount of cells , scroll or down, tick random cells. don't understand logic behind because method use ticking didselectrowatindex doesn't executed (i put breakpoint in there check it). not know causing here cellforrowatindex:

- (uitableviewcell *) tableview: (uitableview *) tableview           cellforrowatindexpath: (nsindexpath *) indexpath {          static nsstring *cellname = @"prefstableviewcell";          uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:                                  cellname];         if (cell == nil)         {                 cell = [[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellname] autorelease];         }          nsuinteger row = [indexpath row];         drink *drink = [drinks objectatindex:row];          cell.textlabel.text = drink.name;         cell.detailtextlabel.text = [nsstring stringwithformat:@"%d", drink.caffeine];         [[cell imageview] setimage:drink.image];          return cell; } 

i'm having feeling has can't tell.

this looks prior scrolling:

enter image description here

this happens when scroll top:

enter image description here

i did not click top one. if scroll down , again it's unticked , 1 ticked.

it may important here didselectrowatindexpath method:

- (void) tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {         uitableviewcell *cell = [tableview cellforrowatindexpath: indexpath];          nslog(@"testing");          if ((cell.accessorytype == uitableviewcellaccessorynone) && [[datacontainersingleton thedatacontainersingleton].favourites count] < 4)         {                 cell.accessorytype = uitableviewcellaccessorycheckmark;                  [[datacontainersingleton thedatacontainersingleton].favourites addobject:[drinks objectatindex:indexpath.row]];                 nslog(@"checked %d", [[datacontainersingleton thedatacontainersingleton].favourites count]);         }         else if (cell.accessorytype == uitableviewcellaccessorycheckmark)         {             (int = 0; < [[datacontainersingleton thedatacontainersingleton].favourites count]; i++)             {                 nsstring *drinkname = [[drinks objectatindex:indexpath.row] name];                 nsstring *favname = [[[datacontainersingleton thedatacontainersingleton].favourites objectatindex:i] name];                  if ([drinkname isequaltostring: favname])                 {                     cell.accessorytype = uitableviewcellaccessorynone;                     [[datacontainersingleton thedatacontainersingleton].favourites removeobjectatindex:i];                     nslog(@"unchecked %d", [[datacontainersingleton thedatacontainersingleton].favourites count]);                 }             }          } } 

you not saving items have been ticked. should save indexes in array, , set tick in cellforrowatindexpath:. like-

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {     [tableview deselectrowatindexpath:indexpath animated:yes];          if(![self.tickedindexpaths containsobject:indexpath]) //tickedindexpaths array             [self.tickedindexpaths addobject:indexpath];         else             [self.tickedindexpaths removeobject:indexpath]; } 

then-

- (uitableviewcell *) tableview: (uitableview *) tableview           cellforrowatindexpath: (nsindexpath *) indexpath {          static nsstring *cellname = @"prefstableviewcell";          uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:                                  cellname];         if (cell == nil)         {                 cell = [[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellname] autorelease];         }          nsuinteger row = [indexpath row];         drink *drink = [drinks objectatindex:row];          cell.textlabel.text = drink.name;         cell.detailtextlabel.text = [nsstring stringwithformat:@"%d", drink.caffeine];         [[cell imageview] setimage:drink.image];          if([self.tickedindexpaths containsobject:indexpath])         {               cell.accessorytype = uitableviewcellaccessorycheckmark;         }         else         {               cell.accessorytype = uitableviewcellaccessorynone;         }          return cell; } 

keep in mind due reuse of cells, last else block important.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -