[英]Custom Table View Cell button stays checked after tableview reloadData
When I reload my tableView, my custom cell button does not reload to its initial state. 当我重新加载tableView时,自定义单元格按钮不会重新加载到其初始状态。 I would like to have the checks return to the "plusImage".
我想让支票退回“ plusImage”。 Any idea why this isnt working?
知道为什么这不起作用吗? Does reloadData envoke new reused cells?
reloadData是否会调用新的重用单元?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ReusableIdentifier = @"Cell";
SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];
cell.delegate = self;
UIImage *checkImage = [UIImage imageNamed:@"check.png"];
UIImage *plusImage = [UIImage imageNamed:@"plusButton"];
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;
}
when the cell is prepareForReuse 当单元格为prepareForReuse时
-(void)prepareForReuse
{
UIImage *plusImage = [UIImage imageNamed:@"plusImage"];
[self.plusButton setBackgroundImage:plusImage forState:UIControlStateNormal];
self.plusButton.enabled = YES;
[super prepareForReuse];
}
Here's how you should use a UIButton's states: 这是您应该如何使用UIButton的状态的方法:
In interface builder or inside your custom class (in this case: SetListTableViewCell
, although I suggest changing that name) you should set the button's background images for different states: UIControlStateNormal
& UIControlStateHighlighted
. 在界面构建器中或在您的自定义类中(在本例中为
SetListTableViewCell
,尽管我建议更改该名称),您应该将按钮的背景图像设置为不同的状态: UIControlStateNormal
和UIControlStateHighlighted
。 Then set the button's state to Normal or Highlighted instead. 然后将按钮的状态设置为“正常”或“突出显示”。 So in
SetListTableViewCell.m
: 因此在
SetListTableViewCell.m
:
- (void)awakeFromNib
{
[super awakeFromNib];
[self.plusButton setBackgroundImage:[UIImage imageNamed:@"plusImage"] forState:UIControlStateNormal];
[self.plusButton setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateHighlighted];
}
-(void)prepareForReuse
{
[super prepareForReuse];
self.plusButton.highlighted = NO;
}
and then: 接着:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ReusableIdentifier = @"Cell";
SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];
cell.delegate = self;
[cell.plusButton setHighlighted: [self.selectedRows containsIndex:indexPath.row]];
cell.tag = indexPath.row;
return cell;
}
If you're using TableView's selection, then you can even use: 如果您使用的是TableView的选择,那么您甚至可以使用:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.plusButton.highlighted = selected;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.