1

I've the following class scheme implemented in obj-c: class A is a subclass of UIViewController. Classes B and C are subclasses of A. Now I need to create a class D that has to be a subclass of A, but it contains a UItableView so it would need UITableViewController functionality too. My question is, how do I solve this problem? Whats the most elegant way of doing this? I've some ideas on how to do it:

1- Make A a subclass of UITableViewController and overload the loadView method on the classes that dont use UItableViews to catch the exeption thrown when se try to load a UITableViewController without a UITableView. (This solution seems to me more like an hack than a solution!)

2- Keep everything unchanged and implement the UITableDelegate and UITableDataSource protocols in D. (This solution seems to me more elegant than the first one but I'm clearly reemplementing UITableViewController).

3- create a UItableViewController inside class D to manage the tableView on behalf of D.

4- Make D a subclass of UITableViewcontroller instead of A and create a data member of type A inside D to manage the A part of D's job.

What do you think is the best way to do it? Transforming A into a protocol is not an option.

Thank you.

3
  • 1
    Objective-C doesn't support multiple inheritance duplicate Commented Aug 16, 2013 at 6:30
  • Option 2 makes the most sense to me. Commented Aug 16, 2013 at 6:32
  • Objective-C does not support multiple inheritance. Use protocols instead. Commented Aug 17, 2013 at 23:43

3 Answers 3

2

The UITableViewController class does a few things for you:

  1. It creates a UITableView as its view, if you haven't specified its view some other way (by using a XIB or a storyboard or by overloading loadView or by assigning to its view property directly). It is easy to do this yourself.

  2. It prevents the current input field from being hidden behind the keyboard when the keyboard appears. You can also do this yourself. It's not terribly hard. Apple documents the process in “Moving Content That Is Located Under the Keyboard” in the Text, Web, and Editing Programming Guide for iOS and there are also many Q&As covering it here on stack overflow.

  3. It does some other incidental things listed in the “Overview” section of the * UITableViewController Class Reference*, all of which are trivial to implement yourself.

  4. If you're using a storyboard, it allows you to define static table view cells in the storyboard. This is the only feature of UITableViewController that I can think of that you cannot easily replicate in your own UIViewController subclass.

If you don't need #4, then you should use your solution #2 (just make an A subclass).

If you need #3, you could use your solution #4. But it would probably be better to factor that functionality out of A into a separate helper class Z, and give both A and D an instance variable (or property) of class Z.

Sign up to request clarification or add additional context in comments.

Comments

1

You do not need a UITableViewController sublcass to handle UITableView's delegate and datasource methods. All you have to do is have your class implement UITableViewDelegate and UITableViewDataSource and then implement the proper methods (ie the ones that a UITableViewController would include).

For example, you could have:

@interface D : A <UITableViewDelegate,UITableViewDataSource>

To declare your UIViewController D as a subclass of A that implements UITableView's dataSource and delegate.

Then, when you create your UITableView in class D, do:

tableView.delegate = self;
tableView.dataSource = self;

Once you have done this, all you have to do is implement the dataSource and delegate methods that you need in class D and your UITableView will be populated.

For example, you could have:

D.m

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.data count]; //self.data is an array the UITableView is populated from.
}

//...etc implement other methods.

Comments

0

The question is not clear.

If A is already a UIViewController then it has a well known property (view) that points to its one and only one view. If D is to be a subclass of A, it can have one and only one view. If what you describe is the case, you have conflicting requirements for D. I would guess it would be better to add a new class E, and have it aggregate A and D, but that's just a guess based on zero information about your actual requirement.

Objective C is not C++ where the 'D' part of the class can own its own 'view' member.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.