【发布时间】:2014-11-03 21:21:00
【问题描述】:
所以,我在 Xcode 中收到“类型 ViewController 不符合协议 UITableViewDataSource”错误。我已经实现了所需的功能:
- cellForRowAtIndexPath
- numberOfRowsInSection
我什至确定我已经告诉 Xcode 有多少部分(这不是必需的)。这是一个单视图应用程序,我已确保我的引用在 Storyboard 中正确设置。所以我的奥特莱斯是我的视图和我的表视图。我的引用插座为 dataSource 和委托设置为我的表视图。
这是代码。
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var items: [String] = ["We", "Dislike", "Swift", "Very", "Much", "Right", "Now"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
}
}
编辑:另外,我知道我不应该需要 tableView.delegate 和 tableView.dataSource,因为我已经在 Storyboard 中分配了它们,但我想我会包括它们以确保你们都知道我知道有他们设置。
【问题讨论】:
标签: ios swift uitableview view