如何在 UIViewController 中与 UITableView 交互? [英] How to interact with UITableView in UIViewController?

查看:23
本文介绍了如何在 UIViewController 中与 UITableView 交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我沉迷于在 Storyboard 中使用 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 继承),并通过拖放将对象库中的 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.

推荐答案

在你的视图控制器 .h 文件中实现 UITableViewDataSourceUITableViewDelegate 协议.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆