在这种情况下如何使用自定义 tableView 单元格处理可重用单元格(从代码中的其他位置更改单元格背景颜色不起作用)
How do I handle reusable cells with custom tableView cells in this situation (changing cell background colour from other location in code not working)
我正在尝试针对我的情况处理出列可重用单元格。我不确定我在这里以最好的方式做事,我能够处理关于将自定义单元格中的文本标签的单元格出队的奇怪事情。我在单元格 xib 代码中用 override func prepareForReuse()
处理了这个问题。然而,当我尝试以同样的方式处理单元格背景时,它并没有像预期的那样工作。我相信这是因为我正在更改 tableView 函数以外的单元格背景...
所以问题是单元格背景没有像预期的那样改变,这是典型的出列可重用单元格问题。如果我将 backgroundColor = nil
放在 prepareForReuse() 中,那么背景根本不会显示。如果我忽略它,那么当我滚动时背景颜色会跳到整个 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)
}
TableView 扩展
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)
}
您需要始终将其设置为合适的。
if shouldSetBackgroundColor {
backgroundColor = .brown
} else {
backgroundColor = nil
}
假设您只初始化了 4 个单元格。
然后显示 4 个单元格。假设所有 shouldSetBackgroundColor
设置为 true
。结果,您看到所有 4 个单元格的背景颜色都变为 brown
。到目前为止一切顺利。
现在是您的第 5 个牢房。如果您没有在单元格上将 shouldSetBackgroundColor
设置为 false
或者没有更改 backgroundColor
颜色的逻辑,那么由于单元格被重新使用, backgroundColor
不会改变。它将保持设置为 brown
...
我正在尝试针对我的情况处理出列可重用单元格。我不确定我在这里以最好的方式做事,我能够处理关于将自定义单元格中的文本标签的单元格出队的奇怪事情。我在单元格 xib 代码中用 override func prepareForReuse()
处理了这个问题。然而,当我尝试以同样的方式处理单元格背景时,它并没有像预期的那样工作。我相信这是因为我正在更改 tableView 函数以外的单元格背景...
所以问题是单元格背景没有像预期的那样改变,这是典型的出列可重用单元格问题。如果我将 backgroundColor = nil
放在 prepareForReuse() 中,那么背景根本不会显示。如果我忽略它,那么当我滚动时背景颜色会跳到整个 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)
}
TableView 扩展
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)
}
您需要始终将其设置为合适的。
if shouldSetBackgroundColor {
backgroundColor = .brown
} else {
backgroundColor = nil
}
假设您只初始化了 4 个单元格。
然后显示 4 个单元格。假设所有 shouldSetBackgroundColor
设置为 true
。结果,您看到所有 4 个单元格的背景颜色都变为 brown
。到目前为止一切顺利。
现在是您的第 5 个牢房。如果您没有在单元格上将 shouldSetBackgroundColor
设置为 false
或者没有更改 backgroundColor
颜色的逻辑,那么由于单元格被重新使用, backgroundColor
不会改变。它将保持设置为 brown
...