views:

574

answers:

4

The typical UITableView usage pattern is to have the main UIViewController become a target datasource and delegate for the UITableView it is holding on to.

Are there any simple and easy to follow tutorials that would help me figure out how to move the code that pertains to the UITableViewDelegate and UITableViewDataSource methods into a separate class and hook that to my UIViewController instead? I would ideally like to have both the delegate and datasource living in the same class.

Right now, I am creating the UITableView via Interface Builder and connecting its outlet to my controller class.

Typical code:

@interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableview *myTableview;
}

I want to do something more like this:

@interface MyController : UIViewController 
{
    IBOutlet UITableview *myTableview;
}
@end

@interface MyTableSourceDelegate : NSObject<UITableViewDelegate, UITableViewDataSource>
{
}

@implementation MyTableSourceDelegate
    // implement all of the UITableViewDelegate and methods in this class 
@end
A: 

You can create separe classes (with UITableViewDelegate , UITableViewDataSource) and add them in IB as external files and link the IBActions

CiNN
I have the separate class that implements UITableViewDelegate, UITableViewDataSource in MyController. How do you link these instances as IBActions?
Alexi Groove
A: 

In IB, you can drag a 'External Object' from Library->Cocoa Touch->Controllers into your xib window. You can then select that object, view the inspector, and set the class. It is now available to serve as a delegate, etc.

sw
Does IB take care of the instantiation of this class? Do I need to do anything to use it? I tried this but my app seems to be crashing due to 'EXC_BAD_ACCESS'.
Alexi Groove
Yep, you're right. I get the same thing now that I can test it out. I've redone this by dragging an Object instead of an External Object. Next, I declared an IBOutlet typed to the Object item's type in the File's Owner controller. I then linked up the target new control's delegate to the new Object, the new Object's view to the new control, and the new IBOutlet to the new Object... and it runs!
sw
FYI the reason for the IBOutlet is that the new control needs to be retained after instantiation. If you don't do this is gets destroyed immediately and essentially you are getting a null pointer exception.
sw
A: 

Hi Justin,

You'll find a very similar question answered here:

http://stackoverflow.com/questions/254354/uitableview-issue-when-using-separate-delegate-datasource

I think you'll find the answers helpful.

Dave

David Peckham
Beautiful -- first read of it seemed like it was going to be a lot to chew but turned out to be very simple to get it working.
Alexi Groove
Cool, I'm glad that helped.
David Peckham
A: 

Hey sw, could you please help me a little more with that.

I have to classes: mapViewController (which is a UIViewController) and tableViewController (which is a TableViewController). I want mapViewController to be the data source for tableViewController.

I have added the UITableViewDataSource protocol and necessary functions to mapViewController. I then open the xib of tableViewController. From the library, I drag a UIViewController into the list of objects and set it's class to mapViewController. Then I right-click the TableView, and drag the "dataSource" outlet to the mapViewController.

This does not work however. Could you please be a bit more specific about what I have to do next to make it work?

Thanks a lot!

Tom