M.Lovegrove
M.Lovegrove

Reputation: 21

How to add border to custom UITableViewCell in ViewWillAppear

I have 2 Table Views with custom Table View Cells, where selecting a cell in the first Table View segues to the 2nd. What I'm trying to achieve is, when you select a cell in the 2nd view, a red border is added and if you go back to the 1st view and return to the 2nd, the cell still has a border.

Before using custom Table View Cells, I tested on 2 Table View Controllers and the code worked; however, it doesn't with custom Table View Cells.

Here is the code I used for Table View Controllers (secondCategory is a class that holds the indexpath of the selected cell):

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    cell?.selectionStyle = UITableViewCell.SelectionStyle.none
    cell?.layer.borderWidth = 3.0
    cell?.layer.borderColor = UIColor.red.cgColor
    secondCategory.currentSelection = indexPath

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    if  secondCategory.currentSelection != nil {
        let cell = tableView.cellForRow(at: secondCategory.currentSelection!)
        cell!.selectionStyle = UITableViewCell.SelectionStyle.none
        cell!.layer.borderWidth = 3.0
        cell!.layer.borderColor = UIColor.red.cgColor
    }
}

Below is the code for the custom Table View Cells (in viewWillAppear, I use tableView(tableView: UITableView, cellForRowAt: IndexPath) because tableView.cellForRow(at: IndexPath) returns nil):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    cell?.selectionStyle = UITableViewCell.SelectionStyle.none
    cell?.layer.borderWidth = 3.0
    cell?.layer.borderColor = UIColor.red.cgColor
    secondCategory.currentSelection = indexPath
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    if secondCategory.currentSelection != nil {
        let currentCell = tableView(secondTable, cellForRowAt: secondCategory.currentSelection!)
        currentCell.selectionStyle = UITableViewCell.SelectionStyle.none
        currentCell.layer.borderWidth = 3.0
        currentCell.layer.borderColor = UIColor.red.cgColor
    }
}

Can someone please tell me why the code for the custom Table View Cells doesn't work?

Upvotes: 1

Views: 1863

Answers (2)

umer farooqi
umer farooqi

Reputation: 572

You should add IBInspectables properties in your UIView Extension, to avoid having junk code in your controller or view file and set border directly through xib or story board.

Here is the code.

    @IBInspectable
    var borderWidth: CGFloat {

        get {
            return layer.borderWidth
        }

        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable
    var borderColor: UIColor? {

        get {

            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            }
            return nil
        }

        set {

            if let color = newValue {
                layer.borderColor = color.cgColor

            } else {
                layer.borderColor = nil
            }
        }
    }

and it will be shown like this:

enter image description here

Moreover, you can also access these properties in swift code with view's reference.

Upvotes: 0

Jorge
Jorge

Reputation: 69

Try to move your logic to cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "IDENTIFIER", for: indexPath) as! CustomTableViewCell

  // TODO: Cell logic

  // Border logic
  if  secondCategory.currentSelection != nil {
    cell.selectionStyle = UITableViewCell.SelectionStyle.none
    cell.layer.borderWidth = 3.0
    cell.layer.borderColor = UIColor.red.cgColor
  }

  return cell
}

Upvotes: 1

Related Questions