【发布时间】:2015-01-28 08:04:33
【问题描述】:
我正在开发一个应用程序,我有一个要求。我必须处理自定义单元格上的收藏按钮,例如。我正在单元格上创建带有图像的自定义按钮,并设置未选择的类型我默认提供的收藏图像一旦用户单击单元格上的收藏按钮,我将更改按钮图像,如选定的收藏夹。我正在使用以下代码
- (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;
}
按钮操作:
-(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;
}
使用上面的代码,我可以将“收藏夹”按钮从正常更改为选中并选择为正常,但问题是当我在第一行选择收藏按钮时,它也会更改 6 和 11 等行。 任何人都可以建议我这样做的正确方法
提前致谢。
【问题讨论】:
标签: ios iphone uitableview