如何在UIViewController中与UITableView进行交互? [英] How to interact with UITableView in UIViewController?
问题描述
我沉迷于在故事板中使用TableViewController,它的类直接继承自UITableViewController,其功能如下:
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
默认情况下你需要实现它们。但是现在我在故事板中使用ViewController(继承自UIViewController),并通过拖放将Object库中的UITableView控件用于视图控制器。显然,我将附加到我的故事板ViewController的类将继承自UIViewController。现在,我如何与视图控制器中UITableView控件中的表格单元格进行交互。想要在上面实现相同的行函数。
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.
推荐答案
实现 UITableViewDataSource
和 UITableViewDelegate
视图控制器.h文件中的协议。
Implement UITableViewDataSource
and UITableViewDelegate
protocols in your view controller .h file.
有一个 UITableView的出口
。
在 viewDidLoad
中,设置tableView的数据源并委托给 self
因为viewcontroller实现了协议。
In viewDidLoad
, set the tableView's datasource and delegate to self
since the viewcontroller implements the protocol.
然后,编写 cellForRowAtIndexPath
方法。它会被调用。
Then, write the cellForRowAtIndexPath
method. It will be getting called.
这篇关于如何在UIViewController中与UITableView进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!