ios 如何设置一个tableview委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2454567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set a tableview delegate
提问by dubbeat
I'm trying to use a UITableView
without using a nib and without using a UITableViewController
.
我正在尝试使用 aUITableView
而不使用笔尖并且不使用UITableViewController
.
I have added a UITableView
instance to a UIViewController
Like So
我UITableView
在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)
此外,我已将以下表格视图方法添加到我的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 {
I am getting a warning saying that my UIViewController
does not implement UITableView
delegate protocol.
我收到一条警告,说我UIViewController
没有实现UITableView
委托协议。
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)
(这是我第一次尝试使用 aUITableView
而不UITableTableview
从新文件选项中选择控制器)
回答by Jacob Relkin
You need to conform your class to the UITableViewDelegate
and UITableViewDataSource
protocols. ( cellForRowAtIndexPath:
is in the UITableViewDataSource
protocol )
您需要使您的课程符合UITableViewDelegate
和UITableViewDataSource
协议。(cellForRowAtIndexPath:
在UITableViewDataSource
协议中)
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.
您在使用 a 时现在而不是之前收到此警告的原因UITableViewController
是因为UITableViewController
已经符合这些协议。
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.
所以,本质上 aUITableViewController
只是一个符合UITableViewDelegate
and的类UITableViewDataSource
,并且有一个UITableView
实例成员。差不多就是这样。
Of course, if you're not already subclassing UITableViewController
, then you need to manually setup the dataSource
and delegate
of the UITableView
:
当然,如果您还没有子类化UITableViewController
,那么您需要手动设置dataSource
和delegate
的UITableView
:
[tableView setDelegate:self];
[tableView setDataSource:self];
回答by ma11hew28
You also must set the dataSource delegate to self:
您还必须将 dataSource 委托设置为 self:
[tableView setDelegate:self];
[tableView setDataSource:self];
or, equally:
或者,同样:
tableview.delegate = self;
tableview.dataSource = self;
回答by Dinesh Reddy Chennuru
[tableview setDataSource:self]
You have to give it definitely, Why because of the two requiredmethods of UITableView
are under UITableViewDataSource
protocol.
你一定要给它,为什么因为这两个必需的方法UITableView
是在UITableViewDataSource
协议下的。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
回答by David Gelhar
The warning is just telling you that your @interface
section should declare that you implement the UITableViewDelegate
protocol:
警告只是告诉你你的@interface
部分应该声明你实现了UITableViewDelegate
协议:
@interface MyUIViewController : UIViewController < UITableViewDelegate >
回答by silentsudo
In Swift3 this is how you set UITableViewDelegate
:
在 Swift3 中,这是您设置的方式UITableViewDelegate
:
class FeedsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var feedsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
feedsTableView.delegate = self
feedsTableView.dataSource = self
// Do any additional setup after loading the view.
}
}