首页 > 解决方案 > 在这种情况下,如何使用自定义 tableView 单元格处理可重用单元格(从代码中的其他位置更改单元格背景颜色不起作用)

问题描述

我正在尝试针对我的情况处理出列可重用单元。我不确定我是否在这里以最好的方式做事,我能够处理关于将单元格出列以获取自定义单元格中的文本标签的奇怪事情。override func prepareForReuse()我在单元格 xib 代码中处理了这个问题。但是,当我尝试以相同的方式处理单元格背景时,它并不能正常工作。我相信这是因为我在 tableView 函数之外的其他地方更改单元格背景......

所以问题是单元格背景没有像预期的那样改变,典型的出列可重用单元格问题。如果我放入backgroundColor = nilprepareForReuse() 那么背景根本不会显示。如果我忽略它,那么当我滚动时,背景颜色会在整个 tableView 上跳跃。

仅供参考,我已在 viewDidLoad() 中正确注册了单元格

我认为这是足够的代码来解释这种情况。感谢任何指针:)

标记单元格背景红色

func markRED(cell: EntrantCell) {
    var timeOut: String {
        let currentDateTime = Date()
        let formatter = DateFormatter()
        formatter.timeStyle = .medium
        return formatter.string(from: currentDateTime)
    }
    if let indexPath = self.entrantsTable.returnIndexPath(cell: cell) {
        entryStatusGrid[indexPath.row] = 2
        entrantTime[indexPath.row].append("OUT- \(timeOut)")
        entrantsTable.cellForRow(at: indexPath)?.backgroundColor = #colorLiteral(red: 0.7647058964, green: 0.01910983652, blue: 0.008289327978, alpha: 1)
        entrantsTable.reloadData()
    }
    print(entrantTime)
    print(entryStatusGrid)
    
    // TODO: Handle proper space status for global button
    Entrant.updateStatus(entryStatusGrid: entryStatusGrid, button: spaceStatus)
}

表视图扩展

extension EntrySession: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return entryGrid.count
}

// TODO: Handle proper deque for reusable cells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let addEntrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.headerCellIdentifier, for: indexPath) as! AddEntrantCell
    let entrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.bodyCellIdentifier, for: indexPath) as! EntrantCell
    
    if indexPath.row == 0 {
        
        addEntrantCell.delegate = self
        addEntrantCell.entrantNameTextField.text = entryGrid[indexPath.row].name
        addEntrantCell.entrantCompanyTextField.text = entryGrid[indexPath.row].company
        return addEntrantCell
    } else {
        
        entrantCell.delegate = self
        entrantCell.nameLabel.text = entryGrid[indexPath.row].name
        entrantCell.companyLabel.text = entryGrid[indexPath.row].company
        return entrantCell
    }
}
}

自定义单元重用()

override func prepareForReuse() {
    super.prepareForReuse()
    name.text = nil
    status.text = nil
    backgroundColor = nil         // When i do this the background color does not change...
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

@IBAction func inButton(_ sender: UIButton) {
    delegate?.markRED(cell: self)
}
@IBAction func outButton(_ sender: UIButton) {
    delegate?.markBLUE(cell: self)
}

标签: iosswiftxcodeuitableview

解决方案


您需要始终将其设置为合适的值。

if shouldSetBackgroundColor {
    backgroundColor = .brown
} else {
    backgroundColor = nil
}

假设您只初始化了 4 个单元格。然后显示 4 个单元格。假设所有都shouldSetBackgroundColor设置为true。结果,您看到所有 4 个单元格的 backgroundColor 为brown. 到现在为止还挺好。

现在是你的第 5 个细胞。如果您没有在单元格上设置shouldSetBackgroundColorfalse,或者没有一些逻辑来更改backgroundColorthen 的颜色,因为单元格被重新使用,backgroundColor则不会改变。它将保持设置为brown...


推荐阅读