我的单元格背景为白色,它们之间通常有很薄的透明分隔线,但我将潜水员设置为红色以突出显示发生的情况。
它一开始是正确的,但是当我滚动时,它会将单元格的颜色更改为分隔线颜色并不断变化。
查看图片以进一步了解我想要的内容。
看看我最终得到的图像。
下面是我的 cellForRowAtIndex 的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for : indexPath) as! CustomCell
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.clear.withAlphaComponent(0.15)
cell.selectedBackgroundView = bgColorView
if indexPath.row % 2 == 0
{
if(indexPath.row == 0){
cell.titleLabel.text = problems[(indexPath as NSIndexPath).row].getTitle()
cell.descriptionLabel.text = problems[(indexPath as NSIndexPath).row].getMessage()
//print("status is \(problems[(indexPath as NSIndexPath).row].status)")
if(problems[(indexPath as NSIndexPath).row].status == 0){
cell.statusLabel.textColor = UIColor.red
}
else{
cell.statusLabel.textColor = UIColor(red: 28/255.0, green: 121/255.0, blue: 125/255.0, alpha: 1.0)
}
cell.statusLabel.text = problems[(indexPath as NSIndexPath).row].calculateSolved()
cell.messageCountLabel.text = "\(problems[(indexPath as NSIndexPath).row].commentCount)"
cell.distanceLabel.text = "\(problems[(indexPath as NSIndexPath).row].getDistance())km"
cell.problemImage.image = #imageLiteral(resourceName: "NoImage")
if(problems[(indexPath as NSIndexPath).row].image_url != ""){
print("runs imageeee")
var storage = FIRStorage.storage()
// This is equivalent to creating the full reference
let storagePath = "http://firebasestorage.googleapis.com\(problems[(indexPath as NSIndexPath).row].image_url)"
var storageRef = storage.reference(forURL: storagePath)
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
storageRef.data(withMaxSize: 30 * 1024 * 1024) { data, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
DispatchQueue.main.async {
cell.problemImage.image = UIImage(data: data!)!
}
print("returned image")
}
}
}
else{
cell.problemImage.image = #imageLiteral(resourceName: "NoImage")
}
cell.backgroundColor = UIColor.white
}
else{
cell.titleLabel.text = problems[(indexPath as NSIndexPath).row - indexPath.row / 2].getTitle()
cell.descriptionLabel.text = problems[(indexPath as NSIndexPath).row - indexPath.row / 2].getMessage()
//print("status is \(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].status)")
if(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].status == 0){
cell.statusLabel.textColor = UIColor.red
}
else{
cell.statusLabel.textColor = UIColor(red: 28/255.0, green: 121/255.0, blue: 125/255.0, alpha: 1.0)
}
cell.statusLabel.text = problems[(indexPath as NSIndexPath).row - indexPath.row / 2].calculateSolved()
cell.messageCountLabel.text = "\(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].commentCount)"
cell.distanceLabel.text = "\(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].getDistance())km"
cell.problemImage.image = #imageLiteral(resourceName: "NoImage")
if(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].image_url != ""){
print("runs imageeee")
var storage = FIRStorage.storage()
// This is equivalent to creating the full reference
let storagePath = "http://firebasestorage.googleapis.com\(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].image_url)"
var storageRef = storage.reference(forURL: storagePath)
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
storageRef.data(withMaxSize: 30 * 1024 * 1024) { data, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
DispatchQueue.main.async {
cell.problemImage.image = UIImage(data: data!)!
}
print("returned image")
}
}
cell.backgroundColor = UIColor.white
}
}
}
else {
cell.backgroundColor = UIColor.red
cell.titleLabel.text = ""
cell.descriptionLabel.text = nil
cell.statusLabel.text = ""
cell.distanceLabel.text = ""
cell.problemImage.image = nil
cell.selectionStyle = .none
}
return cell
}
最佳答案
使用这个:
tableView(_:shouldHighlightRowAt:)
还有这个:
tableView(_:didSelectRowAt:)
并且取消突出显示
tableView(_:didUnhighlightRowAt:)
要修改单元格选择,请不要忘记遵守 UITableViewDelegate。
关于ios - UITableView 滚动快速更改单元格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43684708/