【发布时间】:2015-09-12 21:08:54
【问题描述】:
我使用swift添加了一个UITableview(没有故事板),但是发生了错误,我不知道为什么
错误是
2015-06-26 11:08:14.149 test[62289:4994907] *** 断言失败 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:6245
2015-06-26 11:08:14.152 test[62289:4994907] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 TextCell 的单元格出列 - 必须注册一个 nib 或一个标识符的类或连接故事板中的原型单元'
我不知道怎么解决
来源是:
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
let swiftBlogs = ["a","b","c","d"]
let textCellIndetifier = "TextCell"
var t = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
t.frame = self.view.frame
self.view.addSubview(t)
t.delegate = self
t.dataSource = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return swiftBlogs.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = t.dequeueReusableCellWithIdentifier(textCellIndetifier, forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
cell.textLabel?.text = swiftBlogs[row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
t.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println(swiftBlogs[row])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
标签: ios swift uitableview