ios - Change Button state after selection on Table View -
ios - Change Button state after selection on Table View -
i have app user able , dislike different items on table view. each table view cell has single button, when clicked executes action recorded incrementing count in dictionary have saved. user can click button 1 time again , unlike row, , hence decrement count. when button clicked, next method called:
- (void)next:(uibutton *)button
right using [button setalpha:0.1];
(changing opacity) indicate if liked or not. here problem: reason another table view cell affected/changed, apart target cell! so, example, if phone call method , click on 1st row, additionally, 4th 1 downwards affected.
the liking , unliking called here:
- (void)liketoobject:(bool)liked object:(pfobject*)object { pfquery *query = [pfquery querywithclassname:@"item"]; [query wherekey:@"objectid" equalto:[object objectid]]; [query getfirstobjectinbackgroundwithblock:^(pfobject *foundobject, nserror *error) { if (!error) { if (liked) { int currentlikes = [foundobject[@"likes"] intvalue]; [foundobject setobject:[nsnumber numberwithint:currentlikes+1] forkey:@"likes"]; [foundobject saveinbackground]; [self loadobjects]; [self.tableview reloaddata]; } else { int currentlikes = [foundobject[@"likes"] intvalue]; [foundobject setobject:[nsnumber numberwithint:currentlikes-1] forkey:@"likes"]; [foundobject saveinbackground]; [self loadobjects]; [self.tableview reloaddata]; } } else { [self.tableview reloaddata]; } }]; }
thanks help!
edit: here ui/table view code.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *identifier = @"parseproduct"; pfproducttableviewcell *cell = [tableview dequeuereusablecellwithidentifier:identifier forindexpath:indexpath]; if (!cell) { cell = [[pfproducttableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier]; } pfobject *product = self.objects[indexpath.row]; [cell configureproduct:product]; [cell.orderbutton addtarget:self action:@selector(next:) forcontrolevents:uicontroleventtouchupinside]; cell.orderbutton.tag = indexpath.row; [cell.buybutton addtarget:self action:@selector(buy:) forcontrolevents:uicontroleventtouchupinside]; cell.buybutton.tag = indexpath.row; [cell.sharebutton addtarget:self action:@selector(share:) forcontrolevents:uicontroleventtouchupinside]; cell.sharebutton.tag = indexpath.row; homecoming cell; } - (void)next:(uibutton *)button { pfobject *object = [self.objects objectatindex:button.tag]; if (![[likesdict objectforkey:object.objectid] boolvalue]) { [likesdict setobject:@yes forkey:object.objectid]; [self liketoobject:yes object:object]; } else { [likesdict setobject:@no forkey:object.objectid]; [self liketoobject:no object:object]; } nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; [likesdict writetofile:[documentsdirectory stringbyappendingpathcomponent:@"likes.plist"] atomically:yes]; [button setalpha:0.1]; } - (void)configureproduct:(pfobject *)product { uiimage *backgroundimage = [uiimage imagenamed:@"screen shot 2014-06-16 @ 6.34.53 pm.png"]; uiedgeinsets backgroundinsets = uiedgeinsetsmake(backgroundimage.size.height/2.0f, backgroundimage.size.width/2.0f, backgroundimage.size.height/2.0f, backgroundimage.size.width/2.0f); uiimageview *backgroundimageview = [[uiimageview alloc] initwithimage:[backgroundimage resizableimagewithcapinsets:backgroundinsets]]; self.backgroundview = backgroundimageview; self.imageview.file = (pffile *)product[@"image"]; self.imageview.contentmode = uiviewcontentmodescaleaspectfit; [self.imageview loadinbackground]; self.pricelabel.text = [nsstring stringwithformat:@"$%d", [product[@"price"] intvalue]]; self.likenumberlabel.text = [nsstring stringwithformat:@"%d",[product[@"likes"] intvalue]]; self.textlabel.text = product[@"name"]; self.textlabel.font = [uifont fontwithname:@"helveticaneue-medium" size:17.0f]; self.textlabel.textcolor = [uicolor colorwithred:82.0f/255.0f green:87.0f/255.0f blue:90.0f/255.0f alpha:1.0f]; self.textlabel.shadowcolor = [uicolor colorwithwhite:1.0f alpha:0.7f]; self.textlabel.shadowoffset = cgsizemake(0.0f, 0.5f); self.textlabel.backgroundcolor = [uicolor clearcolor]; self.textlabel.numberoflines = 2; if ([product[@"hassize"] boolvalue]) { self.sizebutton = [uibutton buttonwithtype:uibuttontypecustom]; self.sizebutton.contenthorizontalalignment = uicontrolcontenthorizontalalignmentleft; [self.sizebutton settitle:nslocalizedstring(@"select size", @"select size") forstate:uicontrolstatenormal]; [self.sizebutton settitlecolor:[uicolor colorwithred:95.0f/255.0f green:95.0f/255.0f blue:95.0f/255.0f alpha:1.0f] forstate:uicontrolstatenormal]; [self.sizebutton settitleedgeinsets:uiedgeinsetsmake(0.0f, 16.0f, 0.0f, 0.0f)]; self.sizebutton.titlelabel.font = [uifont fontwithname:@"helveticaneue-medium" size:16.0f]; [self.sizebutton settitleshadowcolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; self.sizebutton.titlelabel.textcolor = [uicolor colorwithred:95.0f/255.0f green:95.0f/255.0f blue:90.0f/255.0f alpha:1.0f]; self.sizebutton.titlelabel.shadowoffset = cgsizemake(0.0f, 0.5f); uiimage *sizeimage = [uiimage imagenamed:@"dropdownbutton.png"]; uiimage *sizepressedimage = [uiimage imagenamed:@"dropdownbuttonpressed.png"]; uiedgeinsets insets = uiedgeinsetsmake(sizeimage.size.height/2, sizeimage.size.width/2, sizeimage.size.height/2, sizeimage.size.width/2); [self.sizebutton setbackgroundimage:[sizeimage resizableimagewithcapinsets:insets] forstate:uicontrolstatenormal]; [self.sizebutton setbackgroundimage:[sizepressedimage resizableimagewithcapinsets:insets] forstate:uicontrolstatehighlighted]; uiimage *arrowimage = [uiimage imagenamed:@"arrow.png"]; uiimageview *arrowview = [[uiimageview alloc] initwithimage:arrowimage]; arrowview.frame = cgrectmake(140.0f, (40.0f - arrowimage.size.height)/2.0f, arrowimage.size.width, arrowimage.size.height); [self.sizebutton addsubview:arrowview]; [self addsubview:self.sizebutton]; } }
i spent bit of time editing question clean code , english. found you're initialising orderbutton
, other buttons in table view initialisation. each button should newly created instance table view cell! assign buttons newly initialised uibutton
instances, , set view properties too, within cellforrowatindexpath:
. reason being, when you're changing opacity of button, button assigned multiple table view cells, other cells change. reason 4th table view cell changing, , wrong, perhaps 1 have scroll , hence reloading views opacity set.
in addition, think writing these settings dictionary , saving in file scheme via plist extremely poor design. subclass pfobject
, have own custom class stores boolean property isliked
, way value stored straight pointer , can assess next actions/steps when retrieve object array , evaluate isliked
(which can change).
ios uitableview ios7 uibutton
Comments
Post a Comment