Z
Z
zzasdee2016-05-03 20:18:36
iOS
zzasdee, 2016-05-03 20:18:36

How to implement same methods working with tableView in UITableViewController and UIViewController?

This question has already been raised ( How to avoid code duplication in two base classes inherited from UIViewController and UITableViewController? ), but it was a long time ago and I decided not to raise an old topic.
I have a lot of different view controllers in my project, I prefer to use them rather than UITableViewController.
In the root subclass for my view controllers (let it be ) there is a regular . There is also a method for handling the appearance of the keyboard and all sorts of other methods where I sometimes use . Everything is fine until you have to use static cells in the table. Then the question arises, how to inherit everything that I have accumulated inBaseViewController : UIViewController@property UITableVIew *tableViewKeyboardWillShow/KeyboardWillHidetableView
BaseViewController? Reasonable remark: bring the whole thing into a category, for UIViewController, but there is a name conflict tableViewfor UITableViewController.
In general, how do you solve this issue?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
ManWithBear, 2016-05-03
@ManWithBear

I do not use tableviewcontroller because there is no need. Static tables are solved by static code.

A
An, 2016-05-04
@Flanker_4

I agree that it is better not to use UITableViewController at all. But I can understand you too, I want to quickly throw a screen in the storyboard and not bother. Here are the solutions I used:
1) for new apps ( >= iOS 9) Use UIStackView. Ideal for this kind of task and does not require unnecessary gestures, which requires uitableview
2) Using a new approach, so promoted by Apple in swift instead of OOP - protocol oriented programming.
You declare your protocol for working with tableView, where you declare the readonly property NSArray *tableViews (who said that there can only be one table on the controller ;) ), or at worst myTableView
You declare your category/extension in swift terminology (or rather a set of them) on UIViewController, where the above protocol and the above property are used.
Further in UITableViewController you implement this protocol

- (NSArray <UITableView*> *) tableViews{
    return @[self.tableView];
}

you do the same in your base BaseViewController (do you need it at all now? think about it. although it’s better to leave it, but in theory it will now become as empty as possible)
Well, in viewDidLoad / init, call the necessary methods from the categories for initialization
It turned out chaotically, but I hope you caught it message

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question