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.
我有一个包含3个UIButtons的自定义UITableViewCell,我希望当我点击一个按钮时,我的单元格的背景会发生变化。但是当我点击一个按钮时,我的UITableView中的所有单元格都会改变它们的背景,而不仅仅是在其中有按钮的单元格。
Custom Cell (.h)
自定义单元格(.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)
自定义单元格(.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)
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
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...
在这种情况下,当我在单元格中点击thanksButton时,所有单元格都将其背景更改为blueColor ...
3 个解决方案
#1
0
(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];
}
#2
0
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.
好吧,我发现自己:不要使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:]但是[[CustomCell alloc] init]并在自定义单元类中创建init函数。
Like this every cells will be a new object.
像这样,每个细胞都将是一个新的对象。
#3
0
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:]
.
添加一个属性(可能是NSDictionary或NSArray),用于跟踪已经挖掘了哪些indexPath。每次创建一个新单元都非常昂贵。我建议你不要放弃[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;
}
#1
0
(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];
}
#2
0
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.
好吧,我发现自己:不要使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:]但是[[CustomCell alloc] init]并在自定义单元类中创建init函数。
Like this every cells will be a new object.
像这样,每个细胞都将是一个新的对象。
#3
0
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:]
.
添加一个属性(可能是NSDictionary或NSArray),用于跟踪已经挖掘了哪些indexPath。每次创建一个新单元都非常昂贵。我建议你不要放弃[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;
}