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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:00:23  来源:igfitidea点击:

how to set a tableview delegate

iphoneiosobjective-cdelegatesuitableview

提问by dubbeat

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

我正在尝试使用 aUITableView而不使用笔尖并且不使用UITableViewController.

I have added a UITableViewinstance to a UIViewControllerLike So

UITableViewUIViewControllerLike 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 UIViewControllerdoes not implement UITableViewdelegate 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 UITableViewwithout selecting the UITableTableviewcontroller from the new file options)

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

回答by Jacob Relkin

You need to conform your class to the UITableViewDelegateand UITableViewDataSourceprotocols. ( cellForRowAtIndexPath:is in the UITableViewDataSourceprotocol )

您需要使您的课程符合UITableViewDelegateUITableViewDataSource协议。(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 UITableViewControlleris because UITableViewControlleralready conforms to these protocols.

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

So, in essence a UITableViewControlleris just a class that conforms to UITableViewDelegateand UITableViewDataSource, and has a UITableViewinstance member. That's pretty much it.

所以,本质上 aUITableViewController只是一个符合UITableViewDelegateand的类UITableViewDataSource,并且有一个UITableView实例成员。差不多就是这样。

Of course, if you're not already subclassing UITableViewController, then you need to manually setup the dataSourceand delegateof the UITableView:

当然,如果您还没有子类化UITableViewController,那么您需要手动设置dataSourcedelegateUITableView

[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 UITableVieware under UITableViewDataSourceprotocol.

你一定要给它,为什么因为这两个必需的方法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 @interfacesection should declare that you implement the UITableViewDelegateprotocol:

警告只是告诉你你的@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.
      }
}