为什么在调用deselectRowAtIndexPath后我的自定义UITableViewCell仍保持选中状态?

时间:2013-11-20 18:00:23

标签: ios objective-c uitableview uiviewcontroller

我有一个UITableViewCell的自定义子类(下面的实现),在弹出子视图控制器后没有被取消选择。我没有使用UITableViewController。我正在使用UIViewController,其视图包含tableView。

以下是尝试取消选择所选单元格的相关viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *selection = [self.tableView indexPathForSelectedRow];
    if (selection) {
        [self.tableView deselectRowAtIndexPath:selection animated:YES];
    }
}

我已经确认,当调用上面的代码时,self.tableView不是nil。

自定义UITableViewCell子类:

@implementation DSCaseCell

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
    }
    return self;
}

- (void)setStatus:(NSString *)status
{
    _status = status;
    self.statusLabelView.backgroundColor = [DSStyles colorForCaseStatus:self.status];
    self.statusLabel.text = [self.status capitalizedString];
}

- (void)layoutSubviews
{
    if ([self.status isEqualToString:DSInteractionClassFacebookComment]) {
        self.iconView.image = [UIImage iconForInteractionClass:self.status];
    } else {
        self.iconView.image = [UIImage iconForInteractionClass:self.messageClass];
    }

    self.statusLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleCaption2];
    self.statusLabelView.layer.cornerRadius = 3;
    self.statusLabelView.backgroundColor = [DSStyles colorForCaseStatus:self.status];
    self.statusLabel.text = [self.status capitalizedString];
    self.statusLabel.textColor = [DSStyles invertedTextColor];
    self.statusLabel.highlightedTextColor = [DSStyles invertedTextColor];

    self.fromLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleLightCaption1];
    self.timeLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleLightCaption1];


    self.descriptionLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleLightCaption1];
    [super layoutSubviews];
}

@end

1 个答案:

答案 0 :(得分:0)

看来这是由tableView:didSelectRowAtIndexPath:

中的以下代码引起的
UITableViewCell *selectedCell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath];

将其更改为此修复问题:

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

不确定我第一次尝试喝的是什么。