[英]UITableView scroll changes cell background color swift
我的細胞帶有白色背景,它們之間通常是很薄的透明分隔線,但是我將潛水員設為紅色以突出顯示發生的情況。
它從右開始,但是當我滾動時,它會將單元格的顏色更改為分隔線顏色,並且一直在變化。
請參閱圖像以進一步了解我想要的內容。
看到我最終得到的圖像。
下面是我的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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.