大家好,今天我正在练习 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/