Friday 15 June 2012

objective c - I need to make a favorite button and change the image when click -



objective c - I need to make a favorite button and change the image when click -

i create button star image , need alter image when clicked. when click button makes favorite , when click 1 time again remove favorite.

here's code

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell"]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cell"]; } favbutton = [uibutton buttonwithtype:uibuttontypecustom]; [favbutton addtarget:self action:@selector(buttonclicked:) forcontrolevents:uicontroleventtouchupinside]; [favbutton setbackgroundimage:[uiimage imagenamed:@"stardes.png"] forstate:uicontrolstatenormal]; favbutton.frame = cgrectmake(200, 90, 20, 25); [cell.contentview addsubview:favbutton]; homecoming cell; }

you have store state somewhere. stored in array (yourdataset) indexed indexpath, noticed don't use. in add-on alter image on click, need set state in array. in buttonclicked method, iterate superview until reach uitableviewcell of clicked button, find position in table. have index of info in array changed.

after that, check state before set background image. this:

mydata* dta = self.mydata[ indexpath.row ]; if (dta.favorite) [favbutton setbackgroundimage:[uiimage imagenamed:@"stardes.png"] forstate:uicontrolstatenormal]; else [favbutton setbackgroundimage:[uiimage imagenamed:@"stardes2.png"] forstate:uicontrolstatenormal];

objective-c xcode ios7

No comments:

Post a Comment