1

I'm trying to make my own version of UITableViewController in a UIViewController (for more customization). So in my superclass, I'm implementing both the delegate and datasource and setting the UITableView object delegate and datasource properties to "this". Only problem is I get a compiler warning complaining that I haven't implemented the mandatory dataSource and delegate methods. I'd like to be able to implement these methods when I subclass my custom UITableViewController.

Is there a neat way to make these warnings go away, or is the only way for me to put empty versions of the mandatory delegate methods, and then override them in the subclass? Is this bad practice? Anyone have any insight on how Apple accomplishes this with there UITableViewController class?

5
  • could you please tell a bit more on what do you mean by "more customization"? maybe there are ways to do what you want better than making your own version of "UITableViewController" Feb 2, 2012 at 21:36
  • Well, a big thing is I want it to ineherit from a UIViewController class that sets up all of my apps viewcontrollers.
    – Ser Pounce
    Feb 2, 2012 at 21:40
  • you can set up a UITableView in a UIViewController with no problem, you don't need a UITableViewController Feb 2, 2012 at 21:43
  • That's exactly what I'm doing, I'm implementing my own "UITableViewController" in a UIViewController. I'm not inheriting from UITableViewController, just trying to mimic it.
    – Ser Pounce
    Feb 2, 2012 at 21:48
  • alright, then if you are implementing the delegate and the datasource you have to stick to the requirements of the protocol and add the necessary methods ... you won't get away with empty methods Feb 2, 2012 at 21:54

1 Answer 1

3

I use this pattern for “abstract” methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [self doesNotRecognizeSelector:_cmd];
    abort();
}

The _cmd variable is the selector of the current method. It's automatically provided, just like self.

You have to call abort() because the compiler knows that abort() doesn't return, but it doesn't know that about doesNotRecognizeSelector:.

At least in iOS 5, the UITableViewController methods aren't empty, because UITableViewController supports loading predefined (static) rows from a storyboard.

6
  • Thanks. Just curious but implementing your abstract pattern, what is the purpose of that versus just leaving the method empty? I would understand it more if it were a method that the user could call, but the delegate method will only be called by the system / delegate. Or maybe I'm missing something, would appreciate it if you could explain.
    – Ser Pounce
    Feb 2, 2012 at 21:50
  • OK I think I see why you did that, to prevent it from firing off a warning for the return value. What if the method had no return value, would you have just left it blank, or done something else?
    – Ser Pounce
    Feb 2, 2012 at 22:45
  • It's possible (though rare) to override doesNotRecognizeSelector: to return, so I always use abort().
    – rob mayoff
    Feb 2, 2012 at 23:02
  • 2
    The purpose of the pattern is to cause an error when the programmer has failed to override a required method. It would be nice to give an error at compile-time, but that's not possible in Objective-C. So instead, this pattern produces an error at runtime if the programmer has failed to override the method in his subclass. The error message isn't the best possible message, but it does include the name of the subclass and the method that needs to be overridden.
    – rob mayoff
    Feb 2, 2012 at 23:03
  • Can you accept the edit than I'll give you credit for the answer? It's just explains it a little more.
    – Ser Pounce
    Feb 3, 2012 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.