How to interact with a UITableView in a UIViewController?

I'm used to using TableViewController in a storyboard whose class directly inherits from UITableViewController and performs the following functions:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

exist by default, and you just need to implement them. But now I use the ViewController (inherited from the UIViewController) in the storyboard and using the UITableView control from the object library to the view controller by dragging and dropping. Obviously, the class that I would connect to my storyboard, the ViewController will be inherited from the UIViewController. Now how can I interact with table cells in a UITableView control in a view controller. Want to implement the same string functions above.

+6
source share
2 answers

Implement the UITableViewDataSource and UITableViewDelegate protocols in your controller .h file.

You have an exit for UITableView .

In viewDidLoad set the viewDidLoad data source and delegate it to self , since the viewcontroller implements the protocol.

Then write the cellForRowAtIndexPath method. He will be called.

+12
source

You must assign the UITableViewDelegate and UITableViewDatasource protocols to your table and implement them. The methods in your question are the methods from these protocols, and they appear automatically when using the UITableViewController.

+2
source

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


All Articles