views:

3981

answers:

3

I think I'm missing something simple, but I can't figure out exactly what it is.

I'm trying to set up an App with a UITabViewController, and one of the Tabs will have a UITableView and UISearchBar (but no Navigation Controller). I set up the UITabViewController with all the tabs in interface builder, and the views are in their own xib files. The xib file for the tab with the UITableView is set up and connected as follows.

Stuff in the browser: File's owner (Class is my custom class that is a child of UITableViewController) view -> View

View (class UIView, reference view -> File's owner) contains: UITableView (if i try and set references to the data source / delegate, the app breaks) UISearchBar (unconfigured at the moment)

This setup displays all the items and doesn't lock up, but I can't assign a DataSource without it crashing when i try and load the tab with the UITableView. What should I do to get data into this table, either in IB or code? My ideas are as follows:

  1. Implement custom UITableView class, hook up to table view in IB or to custom tableviewcontroller in Xcode.
  2. Pound head or laptop against the wall until it works.

Update: Here's the error the simulator pushes to the console when I connect the Tableview's data source and delegate to File Owner (who's class is my custom tableviewcontroller).

2/14/09 6:59:12 PM TabBarWillbeRight[33172] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x523760'

+1  A: 

If you can't assign the DataSource without it crashing, then that suggests that some of the the DataSource delegate methods have not been implemented.

In particular the class you are assigning as the data source should implement UITableViewDataSource

DasBoot
I implemented numberOfSectionsInTableView, numberOfRowsInSection, and cellForRowAtIndexPath, and they all return 1 , 1, and a cell with static cell.text. Did i miss a method?
No that should be it. Though just to be precise it is tableView:numberOfRowsInSection and tableView:cellForRowAtIndexPath
DasBoot
right - that's how they're set up - so what exactly should be my data source/delegate... i've tried setting it with setDataSource: and setDelegate too. Perhaps I'm missing some setup in AppDelegate.m?
I would try to put breakpoints in the datasource and delegate methods to see if they are being called properly. That's the best way to debug this I think.
DasBoot
It only breaks when i try and set datasource and delegate in IB, but when i use this in the custom tableviewcontroller's viewwillappear, i get a working table with no data. [mytableview setDelegate:self]; [mytableview setDatasource:self]; self.tableview = mytableview;
+1  A: 

Are you sure your class implements UITableViewDelegate and UITableViewDataSource classes? Also check the declaration of your viewcontroller. Your .h file should be something like this

@interface MyClass:UIViewController <UITableViewDelegate,UITableViewDataSource>

Everything else seems to be in place. If the class declaration does not specify that it implements UITableViewDelegate and DataSource, you will get the exception as soon as you set the datasource.

lostInTransit
hmmm - that sounds like it might be it. Since then, I've ditched interface builder and created all the relevant methods programatically. The moral of the story: black boxes are no fun.
Even if you do not use IB, make sure the class declaration mentions that the class implements UITableViewDelegate and UITableViewDataSource.
lostInTransit
+7  A: 

I had a similiar Issue with the same error message. The simple solution was to click at the View in the TabBarController (The grey rectangle which says "view loaded from..") in IB and change the class of the element to the classname of my TableViewController.

Andy
Right on the spot! Thanks!
Alexandre L Telles
Worked for me too. Thanks!
lguy
Works for me, awesome!!!! :D
tarnfeld
Thank you soooo much :D
balexandre