iOS 和 Xcode:在 UITableView 中设置委托和数据源时出现不兼容的类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20795298/
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
iOS and Xcode: incompatible type error when setting delegate and datasource in a UITableView
提问by u365975
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:
我正在尝试以编程方式制作一个应用程序,其中包含一个 UITableView,它根据应用程序的 Documents 目录中的文件制作项目列表。我已经能够将文件读入数组_filepathsArray
,但是当我尝试使用数组填充表时,编译会崩溃并且 Xcode 会抛出警告。Xcode 指出了以下几行的问题:
_tableView.delegate = self;
_tableView.dataSource = _filepathsArray;
Both of these throw "semantic issues". The first throws
这两者都会引发“语义问题”。第一次投掷
`Assigning to 'id<UITableViewDataSource>' from incompatible type 'NSArray *__strong'`,
while the second throws
当第二次投掷时
`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.
如果我删除这些行,应用程序将正确编译(但当然不会使用数据来填充表格),所以我认为问题与这些有关。
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.
我是 Objective C 和 Xcode 的初学者,所以我不太清楚我在这里做错了什么。谢谢您的帮助。
UPDATE
更新
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:
我已将行_tableView.dataSource = _filepathsArray;
更改_tableView.dataSource = self;
为如下几个答案所述。现在,两行都抛出相同的错误:
`Assigning to 'id<UITableViewDelegate>' from incompatible type 'BrowserViewController *const __strong'`.
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
这个错误可能是视图控制器配置方式的结果吗?在头文件中,它被定义为一个 UIViewController
@interface BrowserViewController : UIViewController
I then include a UITableView as a subview.
然后我包含一个 UITableView 作为子视图。
回答by san
You should declare a UITableViewDataSource
, which is an object that implements that protocol and supplies data to your table.
您应该声明一个UITableViewDataSource
,它是一个实现该协议并向您的表提供数据的对象。
_tableView.dataSource = self;
From Apple Docs
来自苹果文档
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.
Updated: Please define your class like below as per my first line in the answer that You should declare a UITableViewDataSource
:
更新:请按照我在你应该声明的答案中的第一行定义你的类,如下所示UITableViewDataSource
:
@interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
回答by Retro
_tableView.dataSource = _filepathsArray;
// <- this is the problem because its type is controller not array,
_tableView.dataSource = _filepathsArray;
// <- 这是问题所在,因为它的类型是控制器而不是数组,
// add this to your interface
@interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
because you are currently not confirming the UITableView protocols
因为您目前没有确认 UITableView 协议
always use this like
总是像这样使用
_tableView.delegate = self;
_tableView.dataSource = self;
and your _filepathsArray
will be used in numberofRows
delegate for getting the row count and int cellForRowIndexPath
to show the data like
并且您_filepathsArray
将在numberofRows
委托中用于获取行数和 intcellForRowIndexPath
以显示数据,例如
cell.titleLabel.text = _filepathsArray[indexPath.row];
回答by rdelmar
You get these warnings because you didn't declare that you would implement the data source and delegate methods in the .h file of the object that you want to be the data source and delegate. Normally, this would be a subclass of UITableViewController or UIViewController, although any object that implements the protocols could be the data source or delegate. A UITableViewController already conforms to those two protocols, so you don't need to declare anything, but if you're using a UIView controller, you should put this (though it's not strictly necessary) in the .h file:
您收到这些警告是因为您没有声明将在要作为数据源和委托的对象的 .h 文件中实现数据源和委托方法。通常,这将是 UITableViewController 或 UIViewController 的子类,尽管实现协议的任何对象都可以是数据源或委托。UITableViewController 已经符合这两个协议,所以你不需要声明任何东西,但是如果你使用 UIView 控制器,你应该把它(虽然它不是绝对必要的)放在 .h 文件中:
@interface YourCustomClassName : UIViewController <UITableViewDataSource,UITableViewDelegate>
In the .m file you should set self as both the data source and delegate (again, this is the usual pattern, but one object doesn't have to provide both roles):
在 .m 文件中,您应该将 self 设置为数据源和委托(同样,这是通常的模式,但一个对象不必提供这两个角色):
self.tableView.delegate = self;
self.tableView.dataSource = self;