programmatically generate different static UITableviews

1.4k views Asked by At

I've just joined StackOverflow and i'm struggling with a programming requirement with a iPhone app I'm developing in swift. I have a tableview list of different calculators and i would like to segue to another UITableView when a item is clicked to then do the detailed calculations for that tool.

I am likely to have lots of tools in the first table (>20) and so i don't want to use storyboard to draw up each new UITableView static table with a different segue for each one.

I wonder if anyone can give me some advice on how to programmatically code a presentation of a new UITableViewController with static cells when a item is clicked. I don't want to use storyboard so i would need to use code to both manage the presentation as well as the generation of the next UITableViewController with static cells.

I have been able to program a static tableview with a custom class programmatically and linked to a storyboard UITableViewController item but i want to do all this programmatically and cut out storyboard all together.

Any advice you could offer would be greatly appreciated.

Thanks.

1

There are 1 answers

2
Mark On BEST ANSWER

A UITableViewController abstracts some things. It seems like maybe what you want to do is to separate things out and have a little more granular control.

You can do this fairly easily. You need 3 things to make this happen:

  • UITableView
  • UITableViewDataSource
  • UITableViewDelegate

A UITableViewController puts these all together for you. We'll have to create them ourselves.

To do this, we make a View Controller, and inherit UITableViewDataSource and UITableViewDelegate

class Example: UIViewController {


}

// MARK - UITableViewDataSource
extension Example: UITableViewDataSource {
    // We need to implement some methods here
}

// MARK - UITableViewDelegate
extension Example: UITableViewDelegate {

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        // Handle the user clicking an item here
    }

}

Three things left to do:

  • Create and show the table
  • Get the data to display in the table
  • Implement the delegate methods

 Creating the table

You should decide if you want to completely programatically create a UITableView, or have Interface Builder lay one out for you in a .xib, and you just link it up via an IBOutlet.

Assuming you want to do the former, you can do the following:

var table: UITableView?

override func viewDidLoad() {
    super.viewDidLoad()
    table = UITableView(frame: view.bounds)
    view.addSubview(table!)
    table?.delegate = self
    table?.dataSource = self
}

 Get the data

When you push this view controller from your previous view controller, be sure to set a variable on this view controller with your data. Assuming you have an array, it's as simple as something like:

exampleViewController.myData = someArray;
navigationController?.pushViewController(exampleViewController, animated: true)

(be sure to create the myData variable in your View Controller to take this)

Implement the delegate methods

Now we can implement the delegate methods to show the data. You may already be familiar this, but for the sake of completeness:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // You should really use dequeueReusableCellWithIdentifier here.
    var cell = UITableViewCell()
    cell.textLabel!.text = myData[indexPath.row]
    return cell
}