1. Doing the exact same thing as you.
2. Reimplementing UITableViewController's functionality on top of UIViewController. In other words, if I had CustomViewController : UIViewController, I would then create:
@interface CustomTableViewController : CustomViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
}
That sucks too.
Categories won't cut it. Multiple inheritance doesn't exist.
I suppose you could also do something horrifying involving preprocessor macros, sort of along the lines of how SynthesizeSingleton injects a bunch of code into classes that use it.
> Greetings,
>
> I have created custom subclasses for both UIViewController and
> UITableViewController.
[snip]
>
> These classes are almost completely identical. They contain many of
> the same properties and methods. Their only differences are centered
> on UITableView-specific code in the UITableViewController subclass.
>
> Is there any good, simple way to avoid duplicate code in this
> scenario? Keeping these classes in sync has become more of a pain,
> lately.
Can your table view controller work as a subclass of your custom view controller?
I.e. your inheritance hierarchy becomes:
UIViewController --> MyUIViewController --> MyTableViewController
If your table view controller really duplicates a lot of view controller functionality, basing one off of the other seems like a decent solution.
Bill
> I've had the same problem. The two (inadequate) solutions I've used are:
>
> 1. Doing the exact same thing as you.
> 2. Reimplementing UITableViewController's functionality on top of UIViewController. In other words, if I had CustomViewController : UIViewController, I would then create:
>
> @interface CustomTableViewController : CustomViewController <UITableViewDataSource, UITableViewDelegate> {
> UITableView *tableView;
> }
>
> That sucks too.
Not sure that I buy the suckiness assertion. Does UITableViewController really do SO much more than the core UIViewController?
@interface CustomTableViewController : CustomViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *_tableView;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@end
@implementation CustomTableViewController
@synthesize tableView = _tableView;
- (void) viewDidLoad {
[super viewDidLoad];
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITableView class]]) {
self.tableView = view;
break;
}
}
}
- (void) viewDidUnload {
[super viewDidUnload];
self.tableView = nil;
}
- (void) dealloc {
[super dealloc];
[_tableView release];
}
@end
> That's definitely possible, though it doesn't seem ideal as I would
> then have to re-implement all of Cocoa's UITableViewController
> functionality. Granted, this probably wouldn't be a lot, but there is
> a list of things UITableViewController does above and beyond
> UIViewController in the UITableViewController documentation. And who
> knows what Apple might be doing that isn't on this list? It seems like
> going down this road might be opening up a can of worms later as I
> find more and more things that MyTableViewController does that
> UITableViewController does not do.
I think you are exaggerating. Reimplementing the UITableViewController functionality you really need shouldn't be more than you've seen in one of the older posts in this thread.
Rafael
@hatfinch
Sent from my iPad (otherwise I would have tested this myself rather than saying "AFAICR"!)
>
> AFAICR, calling -initWithNibName:bundle: on a UITableViewController gives you a view controller with self.tableView = nil. That is to say, you can just drop the UIViewController subclass, and test for self.tableView in cases in where you need differing behaviour.
UITableView throws an exception if its view isn't a UITableView.
There really isn't a lot to it, and the benefits far outweigh the work involved. It would resolve your common UIViewController subclass use-case, and you'd also have complete control over your UIViewController's root view which is important when you're building more complicated interfaces.
Matt Gallagher's re-implementation is pretty thorough, so I'd take a look at that first:
http://cocoawithlove.com/2009/03/recreating-uitableviewcontroller-to.html
Cheers,
Nathan de Vries
UITableViewController is *very* simple to implement.
Why not simply just subclass your custom UIViewController and implement the UITableViewController behaviour that is described in the documentation. I would be surprised if it is more than few screens of code.
S.
<MyViewController.m>
@implementation MyViewController
#include "MyViewControllerCommon.m"
@end
<MyTableViewController,m>
@implementation MyTableViewController
#include "MyViewControllerCommon.m"
// Table view controller specific stuff
@end