I have cells with white backgrounds with what is normally thin clear divider between them, but ive made the diver red to highlight what happens.
It starts off right, but when I scroll it changes the colour of the cells to the divider colour and keeps changing.
See the image for further clarity of what i want.
See the image of what i end up with.
Below is the code from my 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
}
Use this:
tableView(_:shouldHighlightRowAt:)
And this:
tableView(_:didSelectRowAt:)
And unhighlighting
tableView(_:didUnhighlightRowAt:)
To modify your cells selection, don't forget to conform to the UITableViewDelegate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.