我意识到这是一个常见的问题,但我只是没有得到它。我有一个按钮,当按下一个单元格时,会更改为复选标记。但是,按下时,它会将其他重复使用的单元格图像更改为复选标记。我正在使用单元格的委托来操作tableView的VC上的本地数据。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell...
static NSString *ReusableIdentifier = @"Cell";
SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];
cell.delegate = self;
if ([self.selectedRows containsIndex:indexPath.row]) {
[cell.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
}
else {
[cell.plusButton setBackgroundImage:plusImage forState:UIControlStateNormal];
}
cell.tag = indexPath.Row
return cell;
}
自定义单元格类中的方法。
- (IBAction)addSongButtonPressed:(UIButton *)sender
{
[self.delegate addSongButtonPressedOnCell:self];
UIImage *checkImage = [UIImage imageNamed:@"check.png"];
[self.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
}
来自单元格委托的方法。
-(void)addSongButtonPressedOnCell:(id)sender
{
NSInteger index = ((UIButton *)sender).tag;
NSMutableDictionary *track = [self.searchTracks objectAtIndex:indexPath];
[self.selectedRows addIndex:indexPath];
}