ios - Updating cells with UIImageView to show currently selected item -


i using sliding view controller holds main controller on top , slide left menu controller.

the controller on left works menu facebook/pintrest apps etc. uitableview on left.

here setup: cellforrowatindexpath

static nsstring *cellidentifier = @"mainmenucell";  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];      uiimage *arrowimage = [uiimage imagenamed:@"selectedarrow"];     self.arrowselectedview = [[uiimageview alloc] initwithframe:cgrectmake(240, 10, arrowimage.size.width, arrowimage.size.height)];     self.arrowselectedview.image = arrowimage;     [cell.contentview addsubview:self.arrowselectedview];     self.arrowselectedview.hidden = yes; }  ////// // additional glue code here set labels etc.... //////  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];     self.arrowselectedview.hidden = no; } else {     cell.contentview.backgroundcolor = [uicolor colorwithred:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];     self.arrowselectedview.hidden = yes; }  nslog(@"%@",cell.contentview.subviews); return cell; 

there total of 7 cells in 2 sections table view. 1 cell in section 1 (0) , rest in section 2 (1). there 3 cells show main controller on top. once selected update table view show arrow next cell this: enter image description here

here example code for: didselectrowatindexpath

        uiviewcontroller *newtopviewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"profilevc"];          __weak typeof(self) weakself = self;          [self.slidingviewcontroller anchortopviewoffscreento:ecright animations:nil oncomplete:^{             cgrect frame = weakself.slidingviewcontroller.topviewcontroller.view.frame;             weakself.slidingviewcontroller.topviewcontroller = newtopviewcontroller;             weakself.slidingviewcontroller.topviewcontroller.view.frame = frame;             [weakself.slidingviewcontroller resettopviewwithanimations:nil oncomplete:^{                 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];             }];         }]; 

issue/question have strange issue here, if click on different cell works arrow shown on other cell. again works 2 more times, third times randomly decides show uiimageview on cell.

even if step through cell creation process see specific cell (the incorrectly displayed one) boolean currentdisplayed set no, doesn't change arrowselectedview not hidden yet somehow? log out subviews , can see randomly not hidden anymore though specific cell not set not hidden, thinking implemented incorrectly somehow?

the main issue here in -tableview:cellforrowatindexpath: you're creating new image view every time create new cell, storing single ivar. call set or hide image view later in method not guaranteed (indeed quite unlikely) addressing same image view added particular cell's content view.

a more convenient place store in subclass of uitableviewcell's view so:

@interface customtableviewcell : uitableviewcell  @property (strong, readwrite) uiimageview *imageaccessory;  @end  @implementation customtableviewcell  @end  @implementation yourclass  - (id)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {   static nsstring *cellidentifier = @"mainmenucell";    customtableviewcell *cell = (customtableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier];   if (nil == cell) {     cell = [[customtableviewcell 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];      uiimage *arrowimage = [uiimage imagenamed:@"selectedarrow"];     uiimageview *imageview = [[uiimageview alloc] initwithframe:cgrectmake(240, 10, arrowimage.size.width, arrowimage.size.height)];     imageview.image = arrowimage;     cell.accessoryimage = imageview;     [cell.contentview addsubview: cell.accessoryimage];     cell.accessoryview.hidden = yes;   }    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];       cell.accessoryimage.hidden = no;   } else {       cell.contentview.backgroundcolor = [uicolor colorwithred:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];       cell.accessoryimage.hidden = yes;   }    return cell; }  @end 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -