ios - Messing with buttons and TextFields with reusable cells in Xcode -
ios - Messing with buttons and TextFields with reusable cells in Xcode -
i create message board custom table view cells. every cell has button liking message. in facebook app, want inform user pressed button changing image (in background sends code server save liking). if user press 1 time again on changed button, unlike message.
my problem is, if press on button of cell, image change, after scrolling can see other cells same unpressed button changed image. if scroll , down, image changes randomly in cells. problem textfields, if type in cell, text show in other cell. solution problem?
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ messagescell *cell = [tableview dequeuereusablecellwithidentifier:@"cell"]; if (!cell) { cell = [[messagescell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cell"]; } //... [cell.likebutton addtarget:self action:@selector(sendlike:) forcontrolevents:uicontroleventtouchupinside]; //... } - (void)sendlike:(id)sender { uibutton * button = sender; messagescell *cell = (messagescell *)button.superview.superview.superview; nsindexpath *indexpath = [self.messagestableview indexpathforcell:cell]; nslog(@"%@", indexpath); if([cell.likebutton.imageview.image isequal:[uiimage imagenamed:@"notliked"]]) { [button setimage:[uiimage imagenamed:@"liked"] forstate:uicontrolstatenormal]; } else { [button setimage:[uiimage imagenamed:@"notliked"] forstate:uicontrolstatenormal]; } }
the problem you're not seperating info (model) cells (view). shouldn't utilize uitableviewcells store data. cells reused, means when 1 scrolls off screen reused 1 time again row. state of it's child-controls maintained.
you store whether item liked in array, instance. in cellforrowatindexpath determine image should displayed based on items in array, not based on child-control of cell. persist info non-ui structure.
this result in improve mvc separation.
ios objective-c uitableview uibutton
Comments
Post a Comment