How can I subclass a UITableView?

I want to subclass UITableView as I want to create a reusable table view component in my application.

The idea is not to use a delegate for say cellForRowAtIndexPath. I want the table view to receive this call.

I don’t think I want a UITableViewController, since this UITableView that I want to build must live in different UIViewControllers (and this UIViewController can have its own UITableViews).

I have subclassed my UITableView as:

@interface ShareUITableView : UITableView 

but none of his methods are called.

My ShareUITableView is created through the NIB, setting its own class in ShareUITableView. I checked in the code that an instance of ShareUITableView was being created.

My UITableView does not delegate it to the view controller, so no problem.

Any ideas?

+4
source share
3 answers

I think you should still go with the Controller class anyway. I expect that subclassing UITableView will be a tedious job - if possible, with a reasonable amount at all.

There is no problem that the UIViewController / NoViewController implements the delegate and data source and yet assigns a different controller to the specific tableView. note that the data source and delegate must not be subclasses of the UITableViewController.

Look at this answer: Deploy delegate at run time?

My UITableView does not delegate it to the view controller, so no problem.

You need to use a delegate and data source, as TableViews are populated and configured. otherwise, you will have to overwrite all UITableView methods - including private ones, without the need for an AppStore. Re-creating a UITableView without a subclass would be even simpler.

+3
source

If I understand you, you need this class declaration:

 @interface ShareUITableView : UITableView <UITableViewDataSource> 

And then in the class constructor, you must assign the instance itself as your own data source:

 - (id)init { //... self.dataSource = self; //... } 

Of course, the class will have to accept the protocol.

Good luck

+6
source

MyTableView.h

 // MyTableView.h // This overrides the UITableViewDataSource with your own so you can add any methods you would like. @protocol MyTableViewDataSource <UITableViewDataSource> @required // This is where you put methods that are required for your custom table to work (optional) - (int)myRequiredMethod; @optional // This is where you put methods that are optional, like settings (optional) @end // This overrides the UITableViewDelegate with your own so you can add any methods you would like. @protocol MyTableViewDelegate <UITableViewDelegate> @required // This is where you put methods that are required for your custom table to work (optional) @optional // This is where you put methods that are optional, like settings (optional) @end // Make sure you add UITableViewDelegate and UITableViewDataSource implementations. @interface MyTableView : UITableView <UITableViewDelegate, UITableViewDataSource> { // Your customer datasource and delegate. id <MyTableViewDataSource> myDataSource; id <MyTableViewDelegate> myDelegate; } @end 

MyTableView.m

 // MyTableView.m #import "MyTableView.h" @implementation MyTableView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code // This is how you can use your custom method. int i = [myDataSource myRequiredMethod]; } return self; } - (void)awakeFromNib { // This assigns the delegate and datasource you assigned to File Owner in your xib to your custom methods myDataSource = (id<MyTableViewDataSource>)self.dataSource; myDelegate = (id<MyTableViewDelegate>)self.delegate; self.delegate = self; self.dataSource = self; } // This is an example of how to override an existing UITableView method. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // This calls the method implemented in your ViewController. See Below. NSInteger rows = [myDataSource tableView:tableView numberOfRowsInSection:section]; return rows; } @end 

Myviewcontroller.h

 // MyViewController.h #import "MyTableView.h" // Use MyTableViewDataSource and MyTableViewDelegate instead of UITableViewDataSource and UITableViewDelegate @interface MyViewController : UIViewController <MyTableViewDataSource, MyTableViewDelegate> { @end 

Myviewcontroller.m

 // MyViewController.m #import "MyViewController.h" @interface MyViewController () @end @implementation MyViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; } // This method will be overridden by myTableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (int)myRequiredMethod { return 2; } 

A subclass is a great way to make reusable user interface elements.

+5
source

Source: https://habr.com/ru/post/1394793/


All Articles