Reputation: 3011
I want to change image on the button, when button click. That button is placed in table cell. Image is changing successfull. But when table scroll by default after every 5 cell button image also changed, while I didn't changed it. Means new cells in table, unwilling change. This cell is reusable, and created by storyboard.
How to use that button as switch button. How can I achieve this.
- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {
while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
subview = subview.superview;
}
return [self.table indexPathForCell:(UITableViewCell *)subview];
}
- (IBAction)btnCheckedCellPressed:(id)sender{
NSIndexPath *myIP = [self indexPathWithSubview:(UIButton *)sender];
MyWantsTableViewCell* cell = (MyWantsTableViewCell *)[self.table cellForRowAtIndexPath:myIP];
if (cell.btnEdit.currentImage == [UIImage imageNamed:@"checkbox-active"]) {
[cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-deactive"]
forState:UIControlStateNormal];
} else {
[cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-active"]
forState:UIControlStateNormal];
}
// [self.table beginUpdates];
// [self.table reloadRowsAtIndexPaths:@[myIP] withRowAnimation:UITableViewRowAnimationFade];
// [self.table endUpdates];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyWantsTableViewCell *cell=nil;
static NSString *AutoCompleteRowIdentifier = @"MyWantsTableViewCellEdit";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[MyWantsTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imgProduct];
}
cell.selectionStyle = UITableViewCellAccessoryNone;
cell.btnEdit.tag = indexPath.row;
return cell;
}
Upvotes: 1
Views: 4875
Reputation: 12719
Use simple way as I have described below, and use an object to keep the current selection, since it will go if you scroll the UITableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
[cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-active"]
forState:UIControlStateNormal];
[cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-deactive"]
forState:UIControlStateSelected];
MyClass *obj=[arr objextAtIndex:indexPath.row];
cell.btnEdit.selected=obj.selected;
}
- (IBAction)btnCheckedCellPressed:(id)sender{
NSIndexPath *myIP = [self indexPathWithSubview:(UIButton *)sender];
MyWantsTableViewCell* cell = (MyWantsTableViewCell *)[self.table cellForRowAtIndexPath:myIP];
cell.btnEdit.selected=!cell.btnEdit.selected;
MyClass *obj=[arr objectAtIndex:myIP.row];
obj.selected=!obj.selected;
}
Sample
Find sample for help here
Upvotes: 4
Reputation: 1716
1.. Take a NSInteger variable like "selectedBtn" and initialize it with -1, in viewDidLoad().
2.. go to cellForRowAtIndexPath() set button.tag == indexpath.row;
3.. Now in btnCheckedCellPressed() set that selectedBtn variable with tag value of that sender button.
4.. Now again go to cellForRowAtIndexPath() and place a check
if(cell.btnEdit.tag == selectedBtn)
// set selected image on button
else
// set unselected Image
Upvotes: 2