【发布时间】:2015-06-20 19:18:11
【问题描述】:
我正在学习 Swift 中的基本表格视图,并在尝试使用 tableView.dequeueResuableCellWithIdentifier 时遇到了问题。
每次调用此方法时,我都会创建一个新的 UITableViewCell(例如 var cell = UITableViewCell() ),但我知道这是非常低效的,不是最佳实践。现在,使用我在下面粘贴的代码,我在 'var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! 处收到运行时错误! UITableViewCell'。
我已经指定了一个 Prototype 单元,并给它一个重用标识符“单元”。
class ViewController: UIViewController, UITableViewDataSource {
let devCourses = [
("iOS App Dev with Swift Essential Training","Simon Allardice"),
("iOS 8 SDK New Features","Lee Brimelow"),
("Data Visualization with D3.js","Ray Villalobos"),
("Swift Essential Training","Simon Allardice"),
("Up and Running with AngularJS","Ray Villalobos"),
("MySQL Essential Training","Bill Weinman"),
("Building Adaptive Android Apps with Fragments","David Gassner"),
("Advanced Unity 3D Game Programming","Michael House"),
("Up and Running with Ubuntu Desktop Linux","Scott Simpson"),
("Up and Running with C","Dan Gookin") ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devCourses.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let (courseTitle, courseAuthor) = devCourses[indexPath.row]
cell.textLabel?.text = courseTitle
return cell
}
我得到的唯一输出是 (lldb),所以我不知道问题出在哪里。我正在使用 Xcode 6.3.2。
谢谢!
【问题讨论】:
-
你在哪里注册表格视图的单元格标识符?
-
从情节提要中,我在表格视图中选择了原型单元格,然后在属性检查器中的标识符字段中输入了“单元格”。
-
您说您在特定行中有运行时错误,但您唯一的输出是 lldb。你确定你没有在该行设置断点吗?
-
我很尴尬地承认这一点,但这就是我所做的 Emilio。我不是故意的,所以我一定是不小心做了。谢谢!!
标签: ios swift uitableview