如何设置一个tableview代理 [英] how to set a tableview delegate
问题描述
我试图使用 UITableView
而不使用nib,而不使用 UITableViewController
。
我已经向 UIViewController
添加了一个 UITableView
/ p>
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)部分{
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
我收到一条警告,指出我的 UIViewController
不实现 UITableView
代理协议
正确的方式告诉表格视图,其代理方法是什么?
是我第一次尝试使用 UITableView
而不选择 UITableTableview
控制器从新文件选项)
您需要将您的课程符合 UITableViewDelegate
和 UITableViewDataSource
协议。 ( cellForRowAtIndexPath:
在 UITableViewDataSource
协议中)
通过在类接口定义中使用尖括号来执行此操作:
@interface myViewController:UIViewController< UITableViewDelegate,UITableViewDataSource> {...}
您现在正在收到此警告,而不是之前使用的原因一个 UITableViewController
是因为 UITableViewController
已经符合这些协议。
所以,本质上,一个 UITableViewController
只是一个符合 UITableViewDelegate
和 UITableViewDataSource的类
,并有一个 UITableView
实例成员。
这是很多的。
当然,如果你还没有将 UITableViewController
您需要手动设置 UITableView
的 dataSource
和委托
>:
[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];
这篇关于如何设置一个tableview代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!