How to implement a UITableViewController with multiple prototype cells programmatically in swift

I just started to learn quickly and I created a settings page as shown in the attached image using StoryBoard. Link used: http://shrikar.com/xcode-6-tutorial-grouped-uitableview/

enter image description here

I want to do the same thing programmatically, that is, I want to implement the cellForRowAtIndexPath function , which has a different prototype cell, as shown in the picture below. If possible, provide an example with a link to a sample user interface.

thank

+3


source to share


2 answers


A great start for you is this project created by Apple. It includes many things and is a demo application. This will give you all the general information you need to get started with UITableView

and UITableViewCell

.

1. Create view files (XIB) for each of the custom cells that you want to see in the tableView. While you can use layout design in code, I would encourage you to start with a visual approach and see how it goes, since you should be fluent with things like auto-layout, constraints, sizing classes, etc.

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

3. Create a Cocoa Touch Class (.swift) file for each of the cells (XIB) created in step 1. In these files you must specify the material that you nested in the cell (UILabels, UIImageViews, etc.) ..) ... Don't forget that cells must have the same classes (open XIB files, third icon in the right pane)

4. Create a Cocoa Touch Class (.swift) file for the UIViewController created in the storyboard. Reference (drag and drop) tableView in this Cocoa Touch class. Make sure the UIViewController in storyboard has this set of classes (custom class field, same as for cells in step 3)

5.Refister the 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 using tableView, click the last button in the right pane and drag the "data source" along with the "data delegate" onto the same view controller (yellow circular symbol).

7. Make sure your Cocoa Touch class (created in step 4) conforms to the UITableViewDataSource, UITableViewDelegate protocols (by implementing the required 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 you are not completely sure how to implement these required methods, you can see them in the project that I referenced at the beginning of my post.

+1


source


Please take a look more at dequeuereusablecellwithidentifier

. If you are using Storyboard add UITableViewCell

with reusableIdentifier

.



+1


source







All Articles