objective c - How can we change table-list title color when we tapped on it in ios -


hi beginner in ios , in project have added labels , image on table-list cell ok have added ok

here main requirement when tapped on table-list cell labels colors must change , image need change below image

for have written code that's not working pleas me

my code:-

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{      nsstring *cells=@"cell";     uitableviewcell *cell=[tableview dequeuereusablecellwithidentifier:cells];      if (cell==nil) {         cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cells];     }      strip = [[uiview alloc]initwithframe:cgrectmake(0, 0, 7, cell.frame.size.height)];     strip.backgroundcolor = [uicolor orangecolor];     [cell.contentview addsubview:strip];      titlelbl = [[uilabel alloc] initwithframe:cgrectmake(65, 7, 130, 35)];     titlelbl.text =[right_menu_array objectatindex:indexpath.row];     titlelbl.textcolor=[uicolor darkgraycolor];     titlelbl.font = [uifont systemfontofsize:15];     [cell.contentview addsubview:titlelbl];      img = [[uiimageview alloc] initwithframe:cgrectmake(10, 7, 35, 35)];     img.image = [uiimage imagenamed:[imagearr1 objectatindex:indexpath.row]];     [cell.contentview addsubview:img]; }  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{     titlelbl.textcolor = [uicolor redcolor];     strip.backgroundcolor = [uicolor redcolr];     img.image = [uiimage imagenamed:@"rahul.png"]; }  - (void)tableview:(uitableview *)tableview diddeselectrowatindexpath:(nsindexpath *)indexpath {     titlelbl.textcolor = [uicolor darkgraycolor];     strip.backgroundcolor = [uicolor whitecolor];     img.image = [uiimage imagenamed:@"rahul.png"]; } 

enter image description here

the easy way create custom uitableviewcell , in delegate functions of uitableview instance of customtableviewcell , set labels textcolor.

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:   (nsindexpath *)indexpath{  customtableviewcell *cell =(customtableviewcell *)[tableview   cellforrowatindexpath:indexpath];     cell.titlelbl.textcolor = [uicolor redcolor];     cell.strip.backgroundcolor = [uicolor redcolr];     cell.img.image = [uiimage imagenamed:@"rahul.png"]; } 

ps: there many other work arounds this...it depends on how want implement code.

other way,

//set flag variable

@property (strong ,nonatomic) nsindexpath *selectedindexpath;       - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{        self.selectedindexpath = indexpath;       [tableview reloaddata];       }        -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{      nsstring *cells=@"cell";     uitableviewcell *cell=[tableview dequeuereusablecellwithidentifier:cells];      if (cell==nil) {         cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cells];     }       strip = [[uiview alloc]initwithframe:cgrectmake(0, 0, 7, cell.frame.size.height)];     strip.backgroundcolor = [uicolor orangecolor];       titlelbl = [[uilabel alloc] initwithframe:cgrectmake(65, 7, 130, 35)];     titlelbl.text =[right_menu_array objectatindex:indexpath.row];     titlelbl.textcolor=[uicolor darkgraycolor];     titlelbl.font = [uifont systemfontofsize:15];      img = [[uiimageview alloc] initwithframe:cgrectmake(10, 7, 35, 35)];     img.image = [uiimage imagenamed:[imagearr1 objectatindex:indexpath.row]];      if(indexpath == selectedindexpath)     {         titlelbl.textcolor = [uicolor redcolor];         strip.backgroundcolor = [uicolor redcolr];         img.image = [uiimage imagenamed:@"rahul.png"];     }      [cell.contentview addsubview:strip];     [cell.contentview addsubview:titlelbl];     [cell.contentview addsubview:img];     } 

//this code not tested.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -