【问题标题】:Type 'ProfilesTableViewController' does not conform to protocol 'UITableViewDataSource' in Xcode 6 GM seed类型“ProfilesTableViewController”不符合 Xcode 6 GM 种子中的协议“UITableViewDataSource”
【发布时间】:2014-09-10 13:44:41
【问题描述】:

今天我迁移到 Xcode 6 GM 种子,现在我收到以下错误:

类型“ProfilesTableViewController”不符合协议 'UITableViewDataSource'。

我已经覆盖了numberOfRowsInSectioncellForRowAtIndexPathnumberOfSectionsInTableView
事实上,直到今天一切正常。我注意到当我删除 UITableViewDataSource 时,一切正常,并且没有发生错误。所以 .. 是否有必要再使用 'UITableViewDataSource',或者只是重写其中的函数?

【问题讨论】:

    标签: ios uitableview swift xcode6


    【解决方案1】:

    这段代码编译得很好:

    class ProfileTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("foo", forIndexPath: indexPath) as UITableViewCell
    
        return cell
    }
    }
    

    所以,你绝对可以继承UITableViewController 并定义它的UITableViewDataSource 协议实现。

    请注意,我使用的是 override 关键字,而且我没有使用自动展开的参数——即,不是这样的:

    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    

    这在以前的 Xcode 6 测试版中是正确的。

    【讨论】:

    • 事实是我们俩都是对的......因为你给我的例子是来自SDK......但我需要在irder中对sdk中的代码进行一些小的修改才能使用条件辅助...所以您的代码有效,我的答案中的代码也有效... :)
    【解决方案2】:

    我的猜测是,至少基于错误,您没有实现所有 required methods

    【讨论】:

      【解决方案3】:

      编辑:由于Xcode 6 beta 语法:

      override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
      

      我们必须检查展开的 tableView。所以我用了:

              if let tblView = tableView {
                  //code
              }
      

          if let theIndexPath = indexPath {
                  //code
          }
      

      所以...为了不改变我的整个项目,我重写了这样的函数:

      override func tableView(tableView: UITableView?, didSelectRowAtIndexPath indexPath: NSIndexPath?){
      

      一切正常,除了我的更改 ProfileTableViewController 不符合UITableViewDataSource,我不得不从类定义中删除数据源。

      【讨论】:

      • 这恐怕是不正确的。请检查我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2014-11-14
      相关资源
      最近更新 更多