Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use static cells in UITableView without using Storyboards?

I have an existing ViewController + xib in my project, and now I want to add a UITableView with static cells, like this:

TableView with static cells

But when I drag a UITableView onto my screen I don't have the "Content > Static" menu in the Attributes Inspector.

I've tried making my controller subclass UITableViewController, but that doesn't help -- I still don't get the option to use static cells in Attributes Inspector.

I've looked around on StackOverflow but haven't found any existing answers to this question. All the existing questions relate to Storyboards (which I'm not using) instead of xib files.

My guess is that Xcode does some kind of magic when you add a UITableViewController to a storyboard, but not when you change an existing xib to inherit from UITableViewController.

Any advice how how to add a table view with static cells to an existing xib?

like image 267
Kyle Fox Avatar asked Jun 14 '12 21:06

Kyle Fox


2 Answers

Static table view cells are only available when using storyboards. However, if you aren't using storyboards for your entire UI you can still use them for individual screens instead of a collection of screens.

To do this you can create a UIStoryboard file with a single view controller on it that has it's File's Owner set to your custom view controller subclass. Set the VC's identifier to some value. When you want to display this, get the storyboard and then instantiate your view controller subclass by creating the VC from your storyboard.

UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:@"your storyboard" bundle:nil];
CustomViewController = [tableViewStoryboard instantiateViewControllerWithIdentifier:@"custom identifier"];

You can present this VC as usual in your app.

This is a great way to start using storyboards without having to convert your entire app to use them.

like image 108
Colin Humber Avatar answered Oct 22 '22 12:10

Colin Humber


This is doable without storyboards:

  1. your controller must adopt protocols for tableview delegate and source...UITableViewDelegate, UITableViewDataSource
  2. Add a table view to your .xib; set the table view's style to "grouped"
  3. Connect the table to a property in your controller (let's call it "yourTableView")
  4. Add table view cells to the .xib as separate views...i.e. not as subviews of the table view...they'll be free floating in the .xib. (see example image below)
  5. Add controls / labels etc to the table cells.
  6. Assign unique strings to the "Identifier" field in Attributes for each table view cell.
  7. Connect the table cells to other properties in the controller (sliderCell, switchCell, etc)
  8. Connect the send events for the cells' controls to IBAction-ed methods in the controller - (IBAction) handleSliderCell, etc.
  9. In the viewDidLoad method of your controller, assign the delegate and source of the table view to the controller (self.yourTableControl.delegate = self; self.yourTableControl.dataSource = self;)
  10. implement numberOfSectionsInTableView, numberOfRowsInSection, and cellForRowAtIndexPath in the controller
  11. in tableView:cellForRowAtIndexPath:, return yourFirstCell, yourSecondCell, etc pointers for appropriate indexPath values

Events for controls for the table cells should go to your handler routines...

Example for step 4: xib

like image 28
ordinaryaverageguy Avatar answered Oct 22 '22 12:10

ordinaryaverageguy