如何处理在自定义Tableview单元格iOS中最喜欢的button点击?

我正在开发一个应用程序,我有一个要求。 我必须处理自定义单元格上的“喜欢”button。 我正在创build单元格上的图像的自定义button和设置未select的types我默认给一个用户单击我最喜欢的button单元格上我喜欢的图像我正在改变button图像喜欢所选的最爱。 我正在使用下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @“CustomCell”; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; if (cell == nil) { cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.favButton.tag = indexPath.section; [cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside]; return cell; } 

button操作:

 -(void)handleFavouriteButton:(id)sender { UIButton *button = sender; NSLog(@"selected favourite button tag %li", (long)button.tag); if (button.selected) { [button setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal]; } else{ [button setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal]; } button.selected=!button.selected; } 

使用上面的代码,我可以改变喜爱的button从正常改变select和select正常,但问题是当我select最喜欢的button,第一排它正在改变6和11等..行也。 任何人都可以build议我正确的方式来做到这一点

提前致谢。

这个button的动作和所有的东西都很好看。 您需要将选定的Button索引作为标签保存到NSMutableArray中,如下例所示:

In.h类:

 interface myclass : UIViewController{ } @property (strong, nonatomic) NSMutableArray *arrcontainstate; 

In.m职业:

 - (void)viewDidLoad { [super viewDidLoad]; _arrcontainstate=[NSMutableArray array]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @“CustomCell”; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; if (cell == nil) { cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.favButton.tag = indexPath.row; if ( [_arrcontainstate containsObject:indexPath.row]) { [cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal]; } else { [cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal]; } [cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside]; return cell; } -(void)handleFavouriteButton:(id)sender { UIButton *button = sender; button.selected=!button.selected; NSLog(@"selected favourite button tag %li", (long)button.tag); if (button.selected) { [_arrcontainstate addObject:button.tag]; [button setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal]; } else { [_arrcontainstate removeObject:button.tag]; [button setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal]; } } 

当您重复使用单元格时,该button的图像不会改变。 第一个单元格在第6和第11个单元格中重用,所以button图像保持选中状态。 用这个:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @“CustomCell”; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; if (cell == nil) { cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.favButton.tag = indexPath.section; [cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal]; [cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside]; return cell; }