pprevalon pprevalon - 4 years ago 142
Swift Question

Swift tableView cell subviews flicker (disappear and then reappear) when cell is selected

I have a viewCell with a few different subviews. one of them is a label with an orange background. now when I select the cell in the tableview the label background flicker(disappear and then reappear) to its original i cant see what is causing this issue.

//my did select func
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!

tableView.deselectRow(at: indexPath, animated: true)
let post = RquestInfoArray[indexPath.row]
if post["status"] as! String == "pending" {

let ac = UIAlertController(title: "My Rquest", message: "Options", preferredStyle: .actionSheet)
let detailAction = UIAlertAction(title: "View Detail", style: .default, handler: { (action) in
tableView.deselectRow(at: indexPath, animated: false)
print("view detail pressed")
})
let deleteAction = UIAlertAction(title: "Delete", style: .default, handler: { (action) in
tableView.deselectRow(at: indexPath, animated: false)
self.tableView.reloadData()
print("Delete")
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
tableView.deselectRow(at: indexPath, animated: false)
})

ac.addAction(detailAction)
ac.addAction(deleteAction)
ac.addAction(cancelAction)
self.present(ac, animated: true, completion: nil)
return
}

if post["status"] as! String == "accepted" {
let vc = messageViewController()
let posting = RquestInfoArray[indexPath.row]
let postId = posting["rquestId"] as! String
vc.hidesBottomBarWhenPushed = true
vc.postId = postId

self.navigationController?.pushViewController(vc, animated: true)
}
}

Answer Source

I assume that you have a Custom Cell class with the label with an orange background (lets call it MyTableViewCell).

So you have to override the setHighlighted(_:animated:) function for that cell, and keep the label's background to orange.

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    ...
    label.backgroundColor = <YourOrange>
}

You may need to override setSelected(_:animated:) depending on your code.

You can also just set the cell's UITableViewCellSelectionStyle to UITableViewCellSelectionStyleNone instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download