Take the 2-minute tour ×

I have a MainViewController, and I want to add two UITableView's to it, each with different cells. But I don't want to clog up my MainViewController code by checking in the table delegate methods which table it is, and then acting on it. It gets too messy.

So I thought I would subclass UITableView and let it handle the cellForRow and other table methods by itself, and this way when I want to add a table to MainViewController, all I'd have to is

CustomTable *customTable = [[CustomTable alloc] init];
[self.view addSubview:customTable];

and all the delegate methods would be handled in that class, leaving my MainViewController clutter free.

Am I approaching this wrong? Should I be subclassing UITableViewController instead? What's the difference?

share|improve this question
Why don't you create two subclasses of NSObject that implement the UITableViewDelegate/DataSource, and for each MainViewController you make, you make one of each those classes and let them handle the tables with information you provide? –  EmilioPelaez Apr 20 '12 at 16:42
I don't understand..I only have 1 MainViewController, and 2 tableViews.. –  moby Apr 20 '12 at 16:44
Let's say I create a regular UITableView. I can assign another NSObject subclass to handle that table's delegate methods? Is that what you're saying? How would I do that? –  moby Apr 20 '12 at 16:49
tableView.delegate = table1delegatensobjectclass? –  moby Apr 20 '12 at 16:50
table1delegatensobjectclass - instance, yes. –  Jenox Apr 20 '12 at 16:59
add comment

2 Answers

up vote 7 down vote accepted

When to subclasss UITableView? Not now.

Create two classes, which are member variables of your view controller. Point the table view delegates at each of your two new classes.

In Cocoa you tend to combine classes rather than inherit from them as you usually do in Java and C#.

share|improve this answer
wish i could up vote this twice! it's very easy to get in a rut where the table view delegate is the owning view controller. breaking that habit makes for a lovely answer here. –  danh Apr 20 '12 at 18:10
add comment

In 3 years of professional working as a objective-c programmer, I didn't need to subclass UITableView once, the patterns, cocoa is depending on, — MVC and delegations (with using protocols), are just simple yet strong enough. And populating a tableview is just one of the best examples.

Make sure, you understand all this topics, as otherwise you will find yourself fighting the framework constantly.

share|improve this answer
add comment

Your Answer


