【问题标题】:UITableView does not conform to protocolUITableView 不符合协议
【发布时间】:2019-07-20 20:31:27
【问题描述】:

类型‘TVViewController’不符合协议‘UITableViewDataSource’

我对编程还很陌生,而且错误非常模糊,所以我不知道从哪里开始排除故障。

class WeeksViewController: UIViewController, UITableViewDataSource {

    //Categories
    var categories = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]

    //Number of sections
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return categories.count
    }

    //Defining the headers
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }

    //Number of rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    //Cell as identifier & category as the class
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow
        return cell
    }

}


【问题讨论】:

    标签: swift uitableview uicollectionview


    【解决方案1】:

    您复制并粘贴了 Swift 2 代码。这些是正确的 Swift 3+ 方法

    class WeeksViewController: UIViewController, UITableViewDataSource {
    
        //Categories
        var categories = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]
    
        //Number of sections
        func numberOfSections(in tableView: UITableView) -> Int
            return categories.count
        }
    
        //Defining the headers
        func func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return categories[section]
        }
    
        //Number of rows
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
    
        //Cell as identifier & category as the class
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", for: indexPath) as! CategoryRow
            return cell
        }
    
    }
    

    看看documentation总是值得的

    【讨论】:

      【解决方案2】:

      我认为问题在于numberOfRows 数据源方法的名称拼写错误,cellForRow 签名也有点不对。

      而不是这些:

      func numberOfSectionsInTableView(tableView: UITableView) -> Int {
          return categories.count
      }
      ...
      func func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          // 
      }
      

      你可能想要这些:

      func numberOfSections(in tableView: UITableView) -> Int {
         return categories.count
      }
      ...
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          // code
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多