iphone - Why the UITableViewCell's changing their value in single cell? -


in app, have customize uitableviewcell , using contentview property. problem data in cells going in different cells. example:the data in cell 3 going cell 8 or 9 when scroll bottom or scroll up. here's code iam using in cellforrowatindexpath:

- (uitableviewcell *)tableview:(uitableview *)tv cellforrowatindexpath:(nsindexpath *)indexpath {      static nsstring *cellidentifier = @"cell";     cgfloat fontsize = [uifont systemfontsize];     cgfloat smallfontsize = [uifont smallsystemfontsize];     uitableviewcell *cell = [tv dequeuereusablecellwithidentifier:cellidentifier];     if (cell == nil) {         cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier]autorelease];         mainlabel = [[[uilabel alloc] initwithframe:cgrectmake(5,0,150,42)] autorelease];         mainlabel.font = [uifont systemfontofsize:smallfontsize];         mainlabel.numberoflines = 3;         mainlabel.linebreakmode = uilinebreakmodewordwrap;         mainlabel.backgroundcolor = [uicolor clearcolor];         mainlabel.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleheight;         [cell.contentview addsubview:mainlabel];          secondlabel = [[[uilabel alloc] initwithframe:cgrectmake(160,0,160,42)] autorelease];         secondlabel.font = [uifont systemfontofsize:smallfontsize];         secondlabel.numberoflines = 3;         secondlabel.linebreakmode = uilinebreakmodewordwrap;         secondlabel.backgroundcolor = [uicolor clearcolor];         secondlabel.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexibleheight;         [cell.contentview addsubview:secondlabel];       }     cell.accessorytype = uitableviewcellaccessorynone;     cell.selectionstyle = uitableviewcellselectionstylenone;     cell.textlabel.font = [uifont systemfontofsize:smallfontsize];     cell.detailtextlabel.font = [uifont systemfontofsize:smallfontsize];     nsarray * titles = [[nsarray alloc] initwithobjects:@"project id", @"status", @"approval date", @"closing date", @"country",@"region name", @"env", @"team full name",@"borrower", @"impagency", @"lending cost", @"ibrdplus", nil];      switch (indexpath.section) {         case 0:              mainlabel.text = [titles objectatindex:indexpath.row];             mainlabel.adjustsfontsizetofitwidth = yes;             mainlabel.numberoflines = 4;             mainlabel.linebreakmode = uilinebreakmodewordwrap;              secondlabel.text = [self.basic objectatindex:indexpath.row];             secondlabel.adjustsfontsizetofitwidth = yes;             secondlabel.numberoflines = 4;             secondlabel.linebreakmode = uilinebreakmodewordwrap;             break;         case 1:              mainlabel.text = [[self.allsectors objectatindex:indexpath.row] valueforkey:@"sectorname"];             mainlabel.adjustsfontsizetofitwidth = yes;             mainlabel.numberoflines = 4;             mainlabel.linebreakmode = uilinebreakmodewordwrap;              secondlabel.text = [[self.allsectors objectatindex:indexpath.row] valueforkey:@"sectorpct"];             secondlabel.adjustsfontsizetofitwidth = yes;             secondlabel.numberoflines = 4;             secondlabel.linebreakmode = uilinebreakmodewordwrap;              break;         case 2:              mainlabel.text = [[self.themes objectatindex:indexpath.row] valueforkey:@"theme_name"];             mainlabel.adjustsfontsizetofitwidth = yes;             mainlabel.numberoflines = 4;             mainlabel.linebreakmode = uilinebreakmodewordwrap;              secondlabel.text = [[self.themes objectatindex:indexpath.row] valueforkey:@"themepct"];             secondlabel.adjustsfontsizetofitwidth = yes;             secondlabel.numberoflines = 4;             secondlabel.linebreakmode = uilinebreakmodewordwrap;              break;          default:             break;     }      return cell; } 

this happening because of line (which need have anyway) :

uitableviewcell *cell = [tv dequeuereusablecellwithidentifier:cellidentifier]; 

basically because cells being reused content stays in cell, therefore need clean cell after dequeu , set new contentview.

what happen here though resetting text of secondlabel, when setting variable here:

secondlabel = [[[uilabel alloc] initwithframe:cgrectmake(160,0,160,42)] autorelease]; 

the last cell created secondlabel pointing time, changing same label, therefore suggest this:

uilabel *lblcell; (id label in [cell.contentview subviews]) {             if ([label iskindofclass:[uilabel class]]) {                 lblcell = label;             }         }  lblcell.text = [[self.themes objectatindex:indexpath.row] valueforkey:@"themepct"]; 

hope helps.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -