Search code examples
swifttextviewconstraintsxib

Swift Bottom border of TableViewCell dissapearing


I'm new to Swift programming.I have following UI:


enter image description here

Everytime I click on one of the rows, the line just dissapears and I dont know why. I just discovered that tableView didSelectRowAt gets called everytime a line dissapears.

class ToDoListController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet var tableView: UITableView!
    let toyData = ["1","2","3","4","5"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        let nib = UINib(nibName: "TableViewCell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: "TableViewCell")
        tableView.backgroundColor = UIColor.init(red: 255/255, green: 214/255, blue: 107/255, alpha: 1)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("do nothing")
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
        cell.textField?.text = "just how?!"
        return cell
    }
}


import UIKit
class TableViewCell: UITableViewCell {
    
    @IBOutlet var button:UIButton!
    @IBOutlet var textField:UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
        
        let bottomLine = CALayer()
        bottomLine.frame = CGRect(x: 10, y: self.frame.height, width: self.frame.width+70, height: 0.5)
        bottomLine.backgroundColor = UIColor.init(red: 253/255, green: 50/255, blue: 39/255, alpha: 1).cgColor
        self.layer.addSublayer(bottomLine)
        self.backgroundColor = UIColor.init(red: 255/255, green: 214/255, blue: 107/255, alpha: 1)
    }
}

Solution

  • in your cellForRowAt function add the following :

    cell.selectionStyle = .none
    

    Sometimes that odd behaviour is due to the default selection style.