【问题标题】:iOS and Xcode: incompatible type error when setting delegate and datasource in a UITableViewiOS 和 Xcode:在 UITableView 中设置委托和数据源时出现不兼容的类型错误
【发布时间】:2013-12-27 05:27:28
【问题描述】:

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

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

这两个都会引发“语义问题”。第一次投掷

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

第二次投掷时

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

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

我是 Objective C 和 Xcode 的初学者,所以我不能完全弄清楚我在这里做错了什么。感谢您的帮助。

更新

我已将_tableView.dataSource = _filepathsArray; 更改为_tableView.dataSource = self;,如下面的几个答案所述。现在,这两行都抛出了同样的错误:

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

这个错误可能是视图控制器配置方式的结果吗?在头文件中定义为一个UIViewController

@interface BrowserViewController : UIViewController

然后我包含一个 UITableView 作为子视图。

【问题讨论】:

    标签: ios xcode uitableview incompatibletypeerror


    【解决方案1】:

    您应该声明一个UITableViewDataSource,它是一个实现该协议并向您的表提供数据的对象。

    _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 的答案中的第一行定义您的类,如下所示:

    @interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
    

    【讨论】:

    • 感谢您的回复。我已经进行了更改,但另一个问题仍然存在。我已为问题添加了更多详细信息。
    • 更新了我的答案,请查收。
    • 同意@san,问题是:完全误解了数据源的工作。数据源是符合 UITableViewDataSource 并处理您的真实数据的数据源。因此,如果您的控制器可以处理数据,您只需将 self 设置为数据源。
    【解决方案2】:

    您收到这些警告是因为您没有声明您将在要成为数据源和委托的对象的 .h 文件中实现数据源和委托方法。通常,这将是 UITableViewController 或 UIViewController 的子类,尽管任何实现协议的对象都可以是数据源或委托。 UITableViewController 已经符合这两个协议,所以你不需要声明任何东西,但是如果你使用的是 UIView 控制器,你应该把它(虽然不是绝对必要的)放在 .h 文件中:

    @interface YourCustomClassName : UIViewController <UITableViewDataSource,UITableViewDelegate> 
    

    在 .m 文件中,您应该将 self 设置为数据源和委托(同样,这是通常的模式,但一个对象不必同时提供这两个角色):

    self.tableView.delegate = self;
    self.tableView.dataSource = self; 
    

    【讨论】:

      【解决方案3】:

      _tableView.dataSource = _filepathsArray; //

      // add this to your interface
      @interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
      

      因为你目前没有确认 UITableView 协议

      总是这样使用

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

      您的_filepathsArray 将在numberofRows 委托中用于获取行数和int cellForRowIndexPath 以显示数据,如

      cell.titleLabel.text = _filepathsArray[indexPath.row];
      

      【讨论】:

      • 感谢您的回复。我已经进行了更改,但另一个问题仍然存在。我已为问题添加了更多详细信息。
      • @Retro 如何在界面中添加多个控制器?例如:我已经有一个接口的自定义控制器。现在我想添加 UIViewController&lt;UITableViewDataSource,UITableViewDelegate&gt; 。所以应该是这样的@interface abc: custom&lt;a&gt;,UIViewController&lt;UITableViewDataSource,UITableViewDelegate&gt;
      • 这些 是协议而不是控制器。您可以为您的控制器创建新协议。
      猜你喜欢
      • 1970-01-01
      • 2012-07-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多