在UIButton里面点击UITableViewCell的背景颜色

时间:2015-07-09 08:59:47

标签: ios objective-c iphone uitableview uibutton

我有一个自定义UITableViewCell,其中包含3个UIButtons,我希望当我点按一个按钮时,我的单元格背景会发生变化。但是当我点击一个按钮时,我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时,所有单元格都将其背景更改为blueColor ...

3 个答案:

答案 0 :(得分:0)

(试试这个)使用以下代码更新了你的笑话。

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

添加一个属性(可能是NSDictionaryNSArray),用于跟踪已经挖掘了哪些indexPath。每次创建一个新单元都非常昂贵。我建议你不要放弃[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;    
}