ios - UITableViewDataSource 无法附加到类,swift-ios 中编译错误

标签 ios cocoa-touch uitableview swift

大家好,今天我正在练习 swift,但是当将 tableViewData 源附加到类时,出现了一个奇怪的编译器错误,错误消息是:

" Type 'ViewController' does not conform to protocol 'UITableViewDataSource'" I can't add datasource.please solve this error.

import UIKit


class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{



    @IBOutlet var tableview: UITableView!
     var array=["cat","dog","cow"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
         var Identifier:NSString="cellid"
        let cell: UITableViewCell=tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text=array[indexPath.row];

    }
}

最佳答案

试试这个:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    var Identifier:NSString="cellid"
    let cell: UITableViewCell=tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text=array[indexPath.row];

    return cell
}

而不是:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
     var Identifier:NSString="cellid"
    let cell: UITableViewCell=tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text=array[indexPath.row];

}

关于ios - UITableViewDataSource 无法附加到类,swift-ios 中编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26802149/

相关文章:

ios - 对齐来自 Swift 3 中不同单元格的按钮

iphone - 无法验证本地播放器iOS6

ios - UITableView 与搜索器并在单元格初始加载和搜索 TableView 后选择 UITableViewcell?

iphone - 如何创建无限滚动的UIScrollView?

iphone - iPhone 上的 HTTPS

uitableview - 在手动创建的部分中对核心数据 tableView 行进行分组

ios - swift:从本地 json 文件填充的 uitableview 在滚动时严重断断续续

ios - 搜索栏 - 导航栏中的取消按钮

ios - mapview如何自动缩放

cocoa-touch - 在 iPhone 上的两个 NSDate 之间循环的最简单方法?