How to interact with UITableView in UIViewController?

I am addicted to use TableViewController in storyboard whose class directly inherits from UITableViewController and functions like:

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

are there by default and you just have to implement them. But now I am using a ViewController (inherits from UIViewController) in storyboard and using a UITableView control from Object library into the view controller by drag and drop. Obviously the class I would attach to my storyboard ViewController will be inherited from UIViewController. Now how do I interact with the table cells in the UITableView control in my view controller. Want to implement same row functions above.


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

Have an outlet for the UITableView .

In viewDidLoad , set the tableView's datasource and delegate to self since the viewcontroller implements the protocol.

Then, write the cellForRowAtIndexPath method. It will be getting called.


You have to assign UITableViewDelegate and UITableViewDatasource protocols to your table and implement it. Methods in your question are methods from these protocols and they appears automatically when you use UITableViewController.

链接地址: http://www.djcxy.com/p/15378.html

上一篇: 如何在UIViewController中设置视图

下一篇: 如何在UIViewController中与UITableView交互?