Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to subclass UITableView?

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?

like image 674
Snowman Avatar asked Aug 30 '26 20:08

Snowman


1 Answers

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#.

like image 52
Stephen Darlington Avatar answered Sep 02 '26 11:09

Stephen Darlington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!