Search code examples
swifttableviewcell

How to access to a static cell


I created a TableView directly in the storyboard; this tableView contains 8 static cells (style basic) in 4 section; now, how could I edit via code these cells? for example for to change textLabel, backgroundColor, separator ecc I tried to set an identifier to each cel but didn't work...


Solution

  • For static cells created in the storyboard, you can simply set the IBOutlet for the elements you want to edit by ctrl-dragging from the storyboard to the corresponding view controller, to end up with something like this:

    class MyViewController: UIViewController {
      @IBOutlet weak var cell1: UITableViewCell!
      @IBOutlet weak var cell2: UITableViewCell!
    }
    

    Then you can access the built-in elements in the Basic view with cell1.textLabel etc.

    Check out the documentation about setting IBOutlets.

    To change the background color, you can do it in the storyboard UI directly or access the backgroundColor property. You might want to read the UITableViewCell Class Reference.