如何设置表格视图委托 [英] how to set a tableview delegate

查看:17
本文介绍了如何设置表格视图委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UITableView 而不使用 nib 并且不使用 UITableViewController.

我已将 UITableView 实例添加到 UIViewController Like So

mytable = [[UITableView alloc] initWithFrame:CGRectMake(22, 207, 270, 233)];[mytable setDelegate:self];[self.view mytable];

我还在 UIViewController 中添加了以下表格视图方法(为简洁起见而删减)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

我收到一条警告说我的 UIViewController 没有实现 UITableView 委托协议.

告诉表格视图其委托方法在哪里的正确方法是什么?

(这是我第一次尝试使用 UITableView 而不从新文件选项中选择 UITableTableview 控制器)

解决方案

您需要使您的类符合 UITableViewDelegateUITableViewDataSource 协议.( cellForRowAtIndexPath:UITableViewDataSource 协议中)

通过在类接口定义中使用尖括号来做到这一点:

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{...}

您现在而不是之前使用 UITableViewController 时收到此警告的原因是因为 UITableViewController 已经符合这些协议.

所以,本质上一个UITableViewController只是一个符合UITableViewDelegateUITableViewDataSource的类,并且有一个UITableView实例成员.差不多就这些了.<​​/p>

当然,如果你还没有继承UITableViewController,那么你需要手动设置dataSourcedelegate>UITableView:

[tableView setDelegate:self];[tableView setDataSource:self];

I'm trying to use a UITableView without using a nib and without using a UITableViewController.

I have added a UITableView instance to a UIViewController Like So

mytable = [[UITableView alloc] initWithFrame:CGRectMake(22, 207, 270, 233)];
[mytable setDelegate:self];
[self.view mytable];

Also I have added the following table view methods to my UIViewController (cut for brevities sake)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

I am getting a warning saying that my UIViewController does not implement UITableView delegate protocol.

Whats the correct way to tell the table view where its delegate methods are?

(This is my first attempt at trying to use a UITableView without selecting the UITableTableview controller from the new file options)

解决方案

You need to conform your class to the UITableViewDelegate and UITableViewDataSource protocols. ( cellForRowAtIndexPath: is in the UITableViewDataSource protocol )

Do this by using angle brackets in your class interface definition:

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}

The reason why you are getting this warning now and not before when you were using a UITableViewController is because UITableViewController already conforms to these protocols.

So, in essence a UITableViewController is just a class that conforms to UITableViewDelegate and UITableViewDataSource, and has a UITableView instance member. That's pretty much it.

Of course, if you're not already subclassing UITableViewController, then you need to manually setup the dataSource and delegate of the UITableView:

[tableView setDelegate:self];
[tableView setDataSource:self];

这篇关于如何设置表格视图委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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