iOS和Xcode:在UITableView中设置委托和数据源时出现不兼容的类型错误 [英] iOS and Xcode: incompatible type error when setting delegate and datasource in a UITableView

查看:240
本文介绍了iOS和Xcode:在UITableView中设置委托和数据源时出现不兼容的类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式制作一个包含UITableView的应用程序,该UITableView根据该应用程序的Documents目录中的文件列出项目列表.我已经能够将文件读入数组_filepathsArray,但是当我尝试使用数组填充表时,编译崩溃并且Xcode发出警告. Xcode指出了以下几行的问题:

I'm trying to make an app programmatically that includes a UITableView that makes a list of items based on the files in the app's Documents directory. I have been able to make the files be read into an array _filepathsArray, but the compilation crashes and Xcode throws warnings when I try to use the array to fill the table. Xcode points out problems with the following lines:

_tableView.delegate = self;
_tableView.dataSource = _filepathsArray;

这两者都引发了语义问题".第一次抛出

Both of these throw "semantic issues". The first throws

`Assigning to 'id<UITableViewDataSource>' from incompatible type 'NSArray *__strong'`,

第二次掷球

`Assigning to 'id<UITableViewDelegate>' from incompatible type 'BrowserViewController *const __strong'`.

如果我删除这些行,则应用程序将正确编译(但当然不会使用数据填充表格),因此我认为问题与这些有关.

If I remove these lines, the app will compile properly (but of course does not use the data to fill the table), so I assume that the problem has to do with these.

我是使用Objective C和Xcode的初学者,所以在这里我不太清楚自己在做什么错.感谢您的帮助.

I'm a beginner with Objective C and Xcode, so I can't quite figure out what I'm doing wrong here. Thanks for the help.

更新

我已将行_tableView.dataSource = _filepathsArray;更改为_tableView.dataSource = self;,如以下几个答案所述.现在,两行都引发相同的错误:

I have changed the line _tableView.dataSource = _filepathsArray; to _tableView.dataSource = self; as explained by several answers below. Now, both lines throw the same error:

`Assigning to 'id<UITableViewDelegate>' from incompatible type 'BrowserViewController *const __strong'`.

此错误是否可能是视图控制器配置方式的结果?在头文件中,它定义为UIViewController

Could this error be the result of the way that the view controller is configured? In the header file, it is defined as a UIViewController

@interface BrowserViewController : UIViewController

然后我将UITableView包含为子视图.

I then include a UITableView as a subview.

推荐答案

您应该声明UITableViewDataSource,这是实现该协议并将数据提供给表的对象.

You should declare a UITableViewDataSource, which is an object that implements that protocol and supplies data to your table.

_tableView.dataSource = self;

来自Apple文档

dataSource
The object that acts as the data source of the receiving table view.
@property(nonatomic, assign) id<UITableViewDataSource> dataSource
Discussion
The data source must adopt the UITableViewDataSource protocol. The data source is not retained.

已更新:请按照答案的第一行定义您的类,如下所示:您应该声明一个UITableViewDataSource:

Updated: Please define your class like below as per my first line in the answer that You should declare a UITableViewDataSource:

@interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

这篇关于iOS和Xcode:在UITableView中设置委托和数据源时出现不兼容的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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