简体   繁体   中英

change background color of UITableViewCell on tap on UIButton inside

I have a custom UITableViewCell who contains 3 UIButtons and I want that when I tap one button, the background of my cell change. But when I tap a button all cells in my UITableView change their background and not only the cell who has the button inside it.

Custom Cell (.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

Custom Cell (.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];
}

In this case when I tap thanksButton in a cell all the cells change their background to blueColor...

(Try this) Updated you touched methood using following code.

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

Ok I found out by my own : don't use [tableView dequeueReusableCellWithIdentifier:forIndexPath:] but [[CustomCell alloc] init] and create your init function in your custom cell class.

Like this every cells will be a new object.

Add a property (maybe an NSDictionary or an NSArray ) for tracking which indexPaths have been tapped already. Creating a new cell every time is very expensive. I suggest you don't abandon [tableView dequeueReusableCellWithIdentifier:forIndexPath:] .

You could try this:

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

Then you can set the background color accordingly:

- (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;    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM