본문 바로가기

iPhone dev./UIView, UITableView

UITableViewController를 UIViewController로 바꿀 때 참고

다음의 링크에서 참조함
http://www.spriing.co.uk/blog/2011/uitableviewcontroller-is-a-waste-of-space/

UITableViewController is a waste of space

UITableViewController is a waste of space

The UITableViewController generated by Xcode is far too restrictive, here’s why…

In every app we’ve developed that has required a UITableView, the data has come from a remote source.  Of course, this means we need to occupy the user while they wait for the data to download.  One option when using UITableViewController is to style up a ‘loading’ UITableViewCell, then reload the tableview once the data is in place.  My preferred method is to display a loading screen, and then hide it once we have the data.  This isn’t possible using UITableViewController.xib – because the UITableView only allows adding subviews to the header and footer.

The other downside I’ve come across is the inability to fix a view to the window without it scrolling as the table view scrolls.  On numerous occasions I’ve needed to add a tab bar to the bottom of a particular view, or attach something to the top or side of the screen.  Once again, this is due to the limitation imposed by the UITableViewController.xib of not being able to add subviews.

All is not lost, there is a very straight forward alternative.  Ditch UITableViewController and start using UIViewController instead.  A few steps are involved, but they’re very quick and will save you time in the long run.

NB: I often start by creating a UITableViewController with Xcode, then modifiying it – it is handy having Xcode generate the table view methods for us.

  1. Create a new UITableViewController class and let Xcode create the associated .xib
  2. In the .h file, change the class to extend UIViewController instead of UITableViewController
  3. Create a new IBOutlet ready for a custom made UITableView (NB: try and avoid the variable name ‘tableView’ so save effort modifying methods in the .m file further down the line.  Something like:
    IBOutlet UITableView *table;
  4. Declare the property:
    @property (nonatomic, retain) UITableView *table;
  5. Move over to the .m file, and create the getters and setters for our ‘table’:
    @synthesize table;
  6. Now jump into the .xib file.  Drag a new UIView from the library into the root of the document window (not inside the UITableView, but next to it)
  7. Drag the UITableView inside (as a subview of) the new UIView we ust created
  8. Click on ‘Files Owner’ and disconnect the ‘view’ variable from the UITableView. Then attach it to the new UIView we created
  9. Still in Files Owner, connect the ‘table’ variable to the ‘Table View’
  10. Build and Run.

Thats it.  Ten easy steps to creating a much more flexible UITableView.  You now have a fully functioning UITableView, but also have the ability to fix views to the screen.

Try it, you won’t go back.

ONE COMMENT

  1. Pierpaolo Caricato says:

    Yeah! I had figured this out as well…
    But you don’t get that cool automatic behavior for removing a table row when you swipe on it or adding a simple edit button if you place your TVC inside a navigation controller! :(
    Have you found any way to achieve this anyway?