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.
The basic idea is to create a new section (rather than a new row) for each array item. The sections can then be spaced using the section header height. Set up your project as described in UITableView example for Swift. (That is, add a UITableView and hook up the tableView outlet to the View Controller).
It's much easier if the table view contains only static cells:
Content
to Static Cells
IBOutlet
s and IBAction
s in the controller class and connect them.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With