metawops
metawops

Reputation: 387

UITableView just for GUI controls, no DataSource

I'm about to write an iOS app in Swift. The main View Controller is a UISplitViewController. The master view of it is a navigation controller whose root view controller is a UITableViewController. The cells in this TableView should only contain GUI elements (UIButton, UISwitch, UITextField etc.). There are quite a few of them so that I want to make use of the scrolling feature of the TableView and want to design a few custom Prototype Cells.

However, I'm still a learner of Swift, Cocoa Touch and all that. I know that a TableView needs a DataSource (and a Delegate) but because I don't have data to display in my cells ... how would my cellForRowAtIndex function look like? How would I even create a few "constant" cells (of prototype 1, prototype 2, ...) in the TableView? I guess it's not possible at design time in IB but only by code? How so?

Thanks so much in advance!

Upvotes: 0

Views: 34

Answers (1)

GIJOW
GIJOW

Reputation: 2353

Setting your tableView delegate in your viewController class is mandatory to override those methods.

You can use numberOfRowsInSection to return a constant value that will be your number of rows.

Inside your cellForRowAt you can verify which row are you in and call appropriated cell.

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     if indexPath.row == 0 {
       //call cell type 1
     }
}

Here you have a reference to create custom cells using xib and call it into your table.

Custom UITableViewCell from nib in Swift

Upvotes: 1

Related Questions