sia
sia

Reputation: 1910

how to implement UITableViewController with multiple prototype cell programmatically in swift

I have just started learning swift and i created a settings page as show in attached image using the StoryBoard. The reference link used is http://shrikar.com/xcode-6-tutorial-grouped-uitableview/

enter image description here

Same thing i want to do it programmatically i.e. i want to implement cellForRowAtIndexPath function which has different prototype cell as shown in below image. If possible please give example with reference to given example UI.

Thanks

Upvotes: 3

Views: 5077

Answers (2)

driver733
driver733

Reputation: 402

A great start for you would be this project created by Apple. It includes a variety of things and is a demo app by itself. This will give all the general info you need to start working with UITableView and UITableViewCell.

1.Create a view (XIB) files for each of the custom cells you wish to see in your tableView. Although you can layout design in code, I would advise you to start out with visual approach and see how goes, since you must be able operate freely with such things as Auto Layout, Constraints, Size Classes, etc.

2.Create a UIViewController with a single table view in it.

3.Create a Cocoa Touch Class file (.swift) for each of the cells (XIBs) you created in step 1. In these files you should reference the stuff you incluled in the cell (UILabels, UIImageViews, etc.). Don`t forget that the cells should have these same classes (open XIB files, third icon on the right panel)

4.Create a Cocoa Touch Class file (.swift) for the UIViewController you created in the storyboard. Reference (mouse drag) the tableView in this Cocoa Touch Class. Make sure that the UIViewController in the storyboard has this class set (Custom class field, same as for cells in step 3)

5.Refister nib for the cell in your ViewDidLoad method

         tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell")
         tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell")
         tableView.rowHeight = UITableViewAutomaticDimension; //<--Calculates rows height
         tableView.estimatedRowHeight = 44.0;                 //   automatically (iOS 8+)

6.On your UIViewController with tableView, click the last button on the right panel and drag "data source" along with "data delegate" to the same view controller (yellow circular symbol).

7.Make sure that your Cocoa Touch class (created in step 4) conforms to the protocols UITableViewDataSource, UITableViewDelegate (by implementing the necessary methods)

class FeedVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
} // <-- your class for the UIViewController with tableView


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}

If your are not completly sure how to implement these required methods, you can look them up in the project I referenced in the beginning of my post.

Upvotes: 1

Shamas S
Shamas S

Reputation: 7549

Please look more into dequeuereusablecellwithidentifier. If you are using Storyboard, add UITableViewCells with reusableIdentifier.

Upvotes: 1

Related Questions