objective c - How to change UILabel text color on Custom UITable cell? -
i've created table view controller on storyboard. want change uilabel text color green when clicked on selected row.
i'm trying this, it's not working:
- (void)viewdidload { [super viewdidload]; menuitems = @[@"home", @"stamp", @"scanner", @"settings"]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *cellidentifier = [menuitems objectatindex:indexpath.row]; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; // remove seperator inset if ([cell respondstoselector:@selector(setseparatorinset:)]) { [cell setseparatorinset:uiedgeinsetszero]; } // prevent cell inheriting table view's margin settings if ([cell respondstoselector:@selector(setpreservessuperviewlayoutmargins:)]) { [cell setpreservessuperviewlayoutmargins:no]; } // explictly set cell's layout margins if ([cell respondstoselector:@selector(setlayoutmargins:)]) { [cell setlayoutmargins:uiedgeinsetszero]; } nslog(@"cell %@",[cell.contentview viewwithtag:1000]); return cell; } - (void) tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{ nsstring *cellidentifier = [menuitems objectatindex:indexpath.row]; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if(indexpath.row == 0){ uilabel *menu= (uilabel*)[cell.contentview viewwithtag:1000]; menu.textcolor = [uicolor greencolor]; nslog(@"cell clicked: %@",[cell.contentview viewwithtag:1000]); } //[cell.textlabel settextcolor:[uicolor greencolor]]; // [self setcellcolor:[uicolor greencolor] forcell:cell]; [self.tableview reloaddata]; }
i've drag label in table cell , set identifier home,stamp,scanner... , change tag 1000.
can tell me why label text color still remain same , provide solution me?
if have multiple labels in cell, need set color separately cell loaded in memory, use
uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];
remove if(indexpath.row == 0)
condition apply color on every cell.
and loop in cell.contentview labels
(if multiple label, can tag, apply unique tag each label , every label tag)
for (id thislabel in cell.contentview.subviews) { if ([thislabel iskindofclass:[uilabel class]]) { uilabel *currentlabel = thislabel; [currentlabel settextcolor:[uicolor greencolor]]; } }
for single label above loop work, easier tag
uilabel *currentlabel= (uilabel*)[cell.contentview viewwithtag:1000]; [currentlabel settextcolor:[uicolor greencolor]];
Comments
Post a Comment