ios - data in tableview is pile up when scrolling -
how create data in tableviewcell when scrolling not pile ?
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *thecell = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:thecell]; if (! cell) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:thecell]; } uilabel *data1 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"nomer"]]withframe:cgrectmake(10, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data1 atindex:0]; uilabel *data2 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"subjek"]]withframe:cgrectmake(50, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data2 atindex:1]; uilabel *data3 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"shadowscore"]]withframe:cgrectmake(200, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data3 atindex:2]; uilabel *data4 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"finalscore"]]withframe:cgrectmake(250, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data4 atindex:3]; uilabel *data5 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"kkm"]]withframe:cgrectmake(300, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data5 atindex:4]; uilabel *data6 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"status"]]withframe:cgrectmake(350, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data6 atindex:5]; uilabel *data7 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"teachernote"]]withframe:cgrectmake(400, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; [cell insertsubview:data7 atindex:6]; return cell; }
the problem everytime cell loaded, adding new uilabel
. becomes problem when cells recycled since have labels created , you're creating more.
you should subclassing uitableviewcell
, creating uilabel
s , layout want on load, setting information in cellforrowatindexpath:
method. allows recycle cells , improve performance while maintaining layouts , avoiding "stacking issue" you've found.
second option
as second, less desirable, option move uilabel
create methods if (!cell)
block , retrieving them tags , setting them outside block. less portable , more fragile, same efficient effect of reusing labels in place. this:
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *thecell = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:thecell]; if (! cell) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:thecell]; uilabel *data1 = [self createlabeltext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"nomer"]]withframe:cgrectmake(10, 10, 150, 50) withfont:[uifont fontwithname:@"arial" size:16] withcolor:[uicolor clearcolor]]; data1.tag = 1; [cell insertsubview:data1 atindex:0]; // ... load other labels , give unique tags } uilabel *data1 = [cell viewwithtag:1]; data1 settext:[nsstring stringwithformat:@"%@",[[arrayutama objectatindex:indexpath.row]objectforkey:@"nomer"]]]; // ... load other labels tag , set text. return cell; }
Comments
Post a Comment