简体   繁体   English

自定义UITableViewCell重用突出显示问题

[英]custom UITableViewCell reuse highlight issue

in the project I'm on, there is already a custom UITableViewCell and I have determined the issue is in the reuse of the cell. 在我正在进行的项目中,已经有一个自定义UITableViewCell,并且我确定问题出在单元的重用中。

These 2 methods are doing the overriding of the highlighting and selecting: 这两种方法正在覆盖突出显示和选择:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    [super setHighlighted:highlighted animated:animated];

    if (self.isCellEditing == NO) {
        if (highlighted) {
            self.customView.backgroundColor = [UIColor redColor];
        } else {
            self.customView.backgroundColor = [UIColor whiteColor];
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    if (self.isCellEditing == NO) {
        if (selected) {
            self.customView.backgroundColor = [UIColor redColor];
        } else {
            self.customView.backgroundColor = [UIColor whiteColor];
        }
    } else {
        if (selected) {
            self.editImageView.image = self.editAccessorySelectedImage;
        } else {
            self.editImageView.image = self.editAccessoryImage;
        }
    }
}

what's happening in the app that is causing the issue is that I need to auto-scroll and select the last selected cell before the app closed on app launch (done in viewDidAppear). 应用中发生问题的原因是,我需要自动滚动并选择上一个选定的单元格,然后应用在应用启动时关闭(在viewDidAppear中完成)。 This works, except for highlighting the cell, it does in fact scroll to the cell selects it, as it shows in the details view (iPad splitview setup), but the cell will not highlight. 这行之有效,除了突出显示该单元格外,实际上它会滚动到选中它的单元格,如它在详细信息视图(iPad splitview设置)中所示,但该单元格不会突出显示。 This is a reuse issue because if the cell that needs to be scrolled to is one of the first cells that are visible at load, it will highlight but if it's a cell that's off screen and it scrolls to that cell it will select but not highlight it. 这是一个重用问题,因为如果需要滚动到的单元格是加载时可见的第一个单元格之一,它将突出显示,但是如果它不在屏幕上并滚动到该单元格,它将选择但不突出显示它。

ETA: cell reuse override: ETA:单元重用覆盖:

- (void)prepareForReuse {

[super prepareForReuse];

self.selectionStyle = UITableViewCellSelectionStyleNone;
_cellEditing = NO;
_swipingToDelete = NO;
_editViewAnimated = NO;
}

In the event anyone else wants a custom highlight and selected state, the fix was: 如果其他人想要自定义突出显示和选定状态,则解决方法是:

if (highlighted || self.isSelected)

this will force it to jump inside this if block since when it's selected you want it the color of the highlight and selected state (assuming they're the same color) 这将迫使它跳到if块中,因为当您选择它时,您希望它是突出显示和选定状态的颜色(假设它们是相同的颜色)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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