【问题标题】:UITableViewController delegate gives error in swiftUITableViewController 委托在 swift 中给出错误
【发布时间】:2014-11-26 09:05:14
【问题描述】:

在 Swift Xcode 6.1 中为 UITableView 编写简单代码时出现以下错误

/Users/classic/Documents/CIPL/Demo Projects/DemoTableView/DemoTableView/ViewController.swift:11:1: Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

/Users/classic/Documents/CIPL/Demo Projects/DemoTableView/UIKit.UITableViewDataSource:3:48: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'

有人知道为什么会出现这个问题吗?

【问题讨论】:

  • 您是否在 Storyboard 中将 TableViewController 定义为 UITableViewcontroller 的子类?

标签: iphone swift


【解决方案1】:

你是否在你的类中声明了 UITableVIewDataSource 协议的强制功能?

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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

【讨论】:

    【解决方案2】:

    您必须为 tableview 实现所有强制协议方法。

     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return arrayData.count
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Define your cell and return it
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多