I know this question is very similar to other questions, but i doesn't resolve the problem using that approach.i follow most of the available solution but it doesn't work for me.. I know that when tableview scrolls it reuse cell but i doesn't know the approach of maintaining the button state . i'll try with following link
How to use Reusable Cells in uitableview for IOS
IOS: Maintaining button state in uitableviewcell
How to Handle Favourite button clicks in custom Tableview cells iOS?
I done all the things.use tags, use touch points and all but nothing seems to work for me.so help me out..and here is my sample code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = (subcategoryCell *)[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([subcategoryCell class]) forIndexPath:indexPath];
/*
* Set button for cell to subscribe with it
*/
cell.btnsubscribe = (UIButton *)[cell.contentView viewWithTag:303];
cell.btnsubscribe.tag = indexPath.row ;
_arraybtnState = [NSMutableArray arrayWithArray:[self.strSubscribe componentsSeparatedByString:@","]];
[cell.btnsubscribe addTarget:self action:@selector(clickBtnSubscribe:) forControlEvents:UIControlEventTouchUpInside];
if (![_arraybtnState containsObject:[NSNumber numberWithInt:indexPath.row]] )
{
[cell.btnsubscribe setImage:[UIImage imageNamed:@"follow"] forState:UIControlStateNormal];
[cell.btnsubscribe setSelected:YES];
}
else {
[cell.btnsubscribe setImage:[UIImage imageNamed:@"following"] forState:UIControlStateNormal];
[cell.btnsubscribe setSelected:NO];
}
return cell;
}
And the click event method...
- (IBAction)clickBtnSubscribe:(id)sender {
UIButton *button = (UIButton*)sender;
NSLog(@"selected button tag %li", (long)button.tag);
NSNumber *tagnum = [NSNumber numberWithLong:(long)button.tag];
if (button.selected) {
[self.arraybtnState addObject:tagnum];
[button setImage:[UIImage imageNamed:@"following"] forState:UIControlStateNormal];
NSLog(@"Subscribe");
[self subscribeButton:button.tag];
[button setSelected:NO];
}
else
{
[self.arraybtnState removeObject:tagnum];
[button setImage:[UIImage imageNamed:@"follow"] forState:UIControlStateNormal];
NSLog(@"unsubscribe");
[self unsubscribeButton:button.tag];
[button setSelected:YES];
}
}
Note: In this code, button is created in storyboard.,but i'll also tried without storyboard too.
0 comments:
Post a Comment