I put a UIView on a storyboard with a view controller and made it the heir of the class I created. This class describes how to draw a table on a view. Here is the class description:

UIView.h

@interface View : UIView @property (strong, nonatomic) UITableView *contentTable; @end 

UIView.m

 @implementation View - (void)drawRect:(CGRect)rect { self.contentTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height) style:UITableViewStylePlain]; [self addSubview:self.contentTable]; } @end 

The table is displayed, but if you assign it a DataSource, it will not work. Code from the view controller:

UIViewController.m

 @interface TRDetailTicketViewController () <UITableViewDataSource> @property (strong, nonatomic) IBOutlet View *mainView; @end - (void)viewDidLoad { [super viewDidLoad]; [self.mainView.contentTable setDataSource:self]; } 

No DataSource method is called (they are described below, did not include them here). You can do this through a method that will return a UITableView, but this is not what I need.

  • assume that you have a dataSource assignment occurs before creating the table. To check this, put a breakpoint or NSLog - Max Mikheyenko
  • @Max Mikheyenko just came to this, but did not understand how to decide. It turned out that the ViewController is first initialized, and then View, so everything I do in the controller is then redefined in the view. - Schmopsel
  • try moving the datasource assignment to viewDidAppear, and immediately reloadData in the same place - Max Mikheyenko
  • Anyway, the view is loaded after viewDidAppear. I reduced the task to a minimum - instead of a table, I added a view and in the controller I try to change its color. Even this does not work, because it is redefined in the view. - Schmopsel
  • then, I would suggest not to reinvent the wheel, and transfer all the code to the controller. approximately as suggested in the answer below - Max Mikheyenko

1 answer 1

It seems not a good idea to initialize a table in drawRect Use the init method better for this (although you need to override all init 'a methods from which the initialization of the table will be called) or awakeFromNib , and set the dimensions in some layoutSubviews . Regarding your question, what is the point of making a table in a view, and what about a datasource and delegate in a controller? In my opinion, transfer the table completely to the controller, or completely in the view and there already assign a datasource \ delegate \ initialization, I would prefer to transfer to the controller.

  • I initialized the table in both drawRect and layoutSubviews , the result is the same. I separate them so that in the UIView class there is only UI rendering, and in the controller its processing. In fact, there will be much more elements, including tables. I simplified to 1 table for example only, in order to understand my problem. And if I do both the drawing and processing in the controller, then we get a bulky controller with a thousand lines of code. - Schmopsel
  • I did not tell you to initialize in layoutSubviews, I said that in this method dimensions were set, initialization should be done in others, in which I wrote above. About a large amount of code, what to do, MVC is such MVC, look towards MVVM. - FedorX