Sylar
Sylar

Reputation: 12072

How to properly implement a static cell with Swift 3

I literally could not find a single tutorial that showed me how to build an app that uses static cells; with clickable cells. Based on few out dated posted and object-c answers, I've put something together. My issue is, when I click on a cell, I get staticDemoTableCell is has no member present.

I have embedded a Table Controller in my UIViewController. For that cell (only one so far), I've created a class:

class staticDemoTableCell: UITableViewCell, UITableViewDelegate {
  @IBOutlet weak var tableView: UITableView! 

  override func awakeFromNib() {
    [...]
    tableView.delegate = self
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("clicked") // Works when the cell is clicked
    // self.present() do not work. I need to present another viewcontroller when this cell is clicked
  }

}

Something does not sit right, for every cell is a class?

I really need to know if I went the correct way about doing this. What I really want is more to this such. Have you seen ie: grouped transactions Monday: a list, Tuesday: a list etc. Each cell will be clickable just like the settings of your iOS device. Any pointers will be highly grateful.

Upvotes: 6

Views: 17809

Answers (3)

Ing. Ron
Ing. Ron

Reputation: 2095

For the rows in the different sections you can do the following by example for 3 sections. Section 1 has 1 row, section 2 has 6 and section 3 has 3 rows:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 3
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        return 1
    case 1:
        return 6
    case 2:
        return 3
    default:
        return 0
    }
    
}

Upvotes: 0

J Arango
J Arango

Reputation: 951

So you can do the following. Set your table view, then make the cells static, once you've done that, you need to make sure you know how many sections you are going to have. This depends on your design or what you want to achieve.

Then you can do something like this:

If you have more than one section, and section 1 has more than one cell. And section 2 has only one cell.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 1:
        switch indexPath.row {
        case 0:
            // Do something
        case 1:
            // Do something 
        default:
            break
        }
    case 2:
        // Do something
    default:
        break
    }
}

If you have only one section with two cells you can do something like:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        // Do something
    case 1:
        // Do something
    default:
        break
    }
}

I hope this helps to solve your problem. Good luck

Upvotes: 9

vadian
vadian

Reputation: 285064

It's much easier if the table view contains only static cells:

  • In Interface Builder select the table view and set the Content to Static Cells
  • Create IBOutlets and IBActions in the controller class and connect them.
  • Implementing table view data source methods and subclassing cells is not needed.

Upvotes: 10

Related Questions