ios - How to reloadCells correctly -
i have navigation menu in uitableview.
my cells loaded following:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = (uitableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; uiview *topsplitterbar = [[uiview alloc] initwithframe:cgrectmake(0, 0, cell.bounds.size.width, 1)]; topsplitterbar.backgroundcolor = [uicolor colorwithred:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1]; [cell.contentview addsubview:topsplitterbar]; } // set boolean determine if item in menu displayed vc on main stack bool currentdisplayed = no; cell.textlabel.backgroundcolor = [uicolor clearcolor]; cell.textlabel.textcolor = [uicolor colorwithred:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1]; cell.textlabel.font = [uifont systemfontofsize:18.0f]; cell.textlabel.shadowcolor = [uicolor colorwithred:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1]; cell.textlabel.shadowoffset = cgsizemake(0, 1); uiview *selectedbg = [[uiview alloc] init]; selectedbg.backgroundcolor = [uicolor colorwithred:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; selectedbg.clipstobounds = yes; uiview *topsplitterbar2 = [[uiview alloc] initwithframe:cgrectmake(0, 0, cell.bounds.size.width, 1)]; topsplitterbar2.backgroundcolor = [uicolor yellowcolor]; [selectedbg addsubview:topsplitterbar2]; [cell setselectedbackgroundview:selectedbg]; // configure cell... if (indexpath.section == 0 ) { if ([self.slidingviewcontroller.topviewcontroller iskindofclass:[mesprofileviewcontroller class]]) { currentdisplayed = yes; } cell.textlabel.text = nslocalizedstring(@"lmdisplayname", @"left menu - displayname"); } else { switch ( indexpath.row ) { case 0: { if ([self.slidingviewcontroller.topviewcontroller iskindofclass:[meshomeviewcontroller class]]) { currentdisplayed = yes; } cell.textlabel.text = nslocalizedstring(@"lmgames", @"left menu - games"); break ; } case 1: { cell.textlabel.text = nslocalizedstring(@"lmshare", @"left menu - share"); break ; } case 2: { cell.textlabel.text = nslocalizedstring(@"lmrate", @"left menu - rate"); break ; } case 3: { if ([self.slidingviewcontroller.topviewcontroller iskindofclass:[messettingsviewcontroller class]]) { currentdisplayed = yes; } cell.textlabel.text = nslocalizedstring(@"lmsettings", @"left menu - settings"); break ; } case 4: { cell.textlabel.text = nslocalizedstring(@"lmhelp", @"left menu - help"); break ; } case 5: { cell.textlabel.text = nslocalizedstring(@"lmlogout", @"left menu - log out"); break ; } } } if (currentdisplayed) { // current item being displayed cell.contentview.backgroundcolor = [uicolor colorwithred:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; uiimage *arrowimage = [uiimage imagenamed:@"selectedarrow"]; uiimageview *arrow = [[uiimageview alloc] initwithframe:cgrectmake(240, 10, arrowimage.size.width, arrowimage.size.height)]; arrow.image = arrowimage; [cell.contentview addsubview:arrow]; } else { cell.contentview.backgroundcolor = [uicolor colorwithred:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0]; nslog(@"%@",cell.textlabel.text); nslog(@"%@",cell.contentview.subviews); } return cell; } then within didselectcell reload set of cells (which can shown current cell) change background.
nsindexpath* displaynamerow = [nsindexpath indexpathforrow:0 insection:0]; nsindexpath* gamesrow = [nsindexpath indexpathforrow:0 insection:1]; nsindexpath* settingsrow = [nsindexpath indexpathforrow:3 insection:1]; nsarray* rowstoreload = [nsarray arraywithobjects:displaynamerow, gamesrow, settingsrow, nil]; [weakself.tableview reloadrowsatindexpaths:rowstoreload withrowanimation:uitableviewrowanimationnone]; what finding uiview *topsplitterbar being created again , added cell.contentview each time cell being reloaded?
i of thought reload of cell clear subviews , create scratch. how should change implementation not continue add these views. have uiimageview arrow added if current cell, same effect above have cells lot of additional views not needed.
background: side view controller (sliding in) menu table view. selected cell shows current selection displayed main top view controller.
you should change implementation of cellforrowatindexpath, doesn't add subviews again , again. have written draft implementation comments.
//here might reusable cell dequeuing subviews there, customization on subviews uitableviewcell *cell = (uitableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { //if haven't got reusable cell create new 1 default subview properties cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; uiview *topsplitterbar = [[uiview alloc] initwithframe:cgrectmake(0, 0, cell.bounds.size.width, 1)]; topsplitterbar.backgroundcolor = [uicolor colorwithred:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1]; [cell.contentview addsubview:topsplitterbar]; } //do customization afterwards, identifying cells based on indexpath ... this neat answer (not accepted one) regarding issue https://stackoverflow.com/a/3490460/837244 accepted answer says solution b work adding subviews repeatedly, mentioned in comments.
Comments
Post a Comment