在点击内部的 UIButton 时更改 UITableViewCell 的背景颜色

change background color of UITableViewCell on tap on UIButton inside

我有一个包含 3 个 UIButtons 的自定义 UITableViewCell,我希望当我点击一个按钮时,我的单元格背景会发生变化。但是当我点击一个按钮时,我 UITableView 中的所有单元格都会更改它们的背景,而不仅仅是其中包含按钮的单元格。

自定义单元格 (.h)

@interface SCActionsViewCell : UITableViewCell

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) UIButton *thanksButton;
@property (nonatomic, strong) UIButton *commentsButton;
@property (nonatomic, strong) UIButton *reButton;


@end

自定义单元格 (.m)

#define TEXT_BUTTON_SIZE 25
@implementation SCActionsViewCell

- (void)awakeFromNib {
    // Initialization code
    self.backgroundColor = [UIColor colorLight];
    _thanksButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
    _commentsButton = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width/3, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
    _reButton = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width*2/3, 0, self.contentView.frame.size.width/3, actionsCellHeight)];

    _thanksButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
    _commentsButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
    _reButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];

    [_thanksButton setTitle:[NSString fontAwesomeIconStringForEnum:FAThumbsOUp] forState:UIControlStateNormal];
    [_commentsButton setTitle:[NSString fontAwesomeIconStringForEnum:FACommentsO] forState:UIControlStateNormal];
    [_reButton setTitle:[NSString fontAwesomeIconStringForEnum:FARetweet] forState:UIControlStateNormal];

    [_thanksButton setTitleColor:[UIColor sGreen] forState:UIControlStateSelected];
    [_reButton setTitleColor:[UIColor sGreen] forState:UIControlStateSelected];

    [self addSubview:_thanksButton];
    [self addSubview:_commentsButton];
    [self addSubview:_reButton];
}

@end

SCViewDataSource (TableviewDataSource)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.posts.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        SCActionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCActionsViewCell" forIndexPath:indexPath];
        cell.tableView = tableView;
        cell.indexPath = indexPath;
        [cell.thanksButton addTarget:(SCViewController *)tableView.delegate action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
}

SCViewController.m

-(void)touched:(UIButton *)button{
    NSLog(@"indexPath : %@",((SCActionsViewCell *)button.superview).indexPath);
    ((SCActionsViewCell *)button.superview).backgroundColor = [UIColor blueColor];
}

在这种情况下,当我点击单元格中的 thanksButton 时,所有单元格的背景都会更改为蓝色...

(试试这个)使用以下代码更新了您接触的方法。

-(void)touched:(UIButton *)button{
    NSLog(@"indexPath : %@",((SCActionsViewCell *)button.superview).indexPath);
    ((SCActionsViewCell *)button.superview).contentView.backgroundColor = [UIColor blueColor];
}

好的,我自己发现了:不要使用 [tableView dequeueReusableCellWithIdentifier:forIndexPath:],而是使用 [[CustomCell alloc] init],并在自定义单元格 class.[=13 中创建 init 函数=]

像这样每个单元格都会是一个新对象。

添加一个 属性(可能是 NSDictionaryNSArray)以跟踪哪些 indexPaths 已经被点击。每次都创建一个新单元非常昂贵。我建议你不要放弃[tableView dequeueReusableCellWithIdentifier:forIndexPath:].

你可以试试这个:

- (void)touched:(UIButton *)sender {
  NSIndexPath *indexPath = ((SCActionsViewCell *)button.superview).indexPath;
  [self.thankedIndexPaths addObject:indexPath];
}

然后你可以相应地设置背景颜色:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  SCActionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCActionsViewCell" forIndexPath:indexPath];
  cell.tableView = tableView;
  cell.thanked = [self.thankedIndexPaths containsObject:indexPath];
  cell.indexPath = indexPath;
  [cell.thanksButton addTarget:(SCViewController *)tableView.delegate action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
  return cell;    
}