【发布时间】:2016-12-07 17:58:51
【问题描述】:
以下行使程序崩溃:
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell
控制台日志显示以下错误:
"caught "NSInternalInconsistencyException", "request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})"
这是我检查过的项目的要点列表:
- 身份检查器中列出了正确的 Storyboard ID
- 身份检查器中列出了正确的自定义类
- 属性检查器在表格视图单元格标识符字段中有“ItemCell”
这里是完整的代码:
import XCTest
@testable import ToDo
class ItemCellTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSUT_HasNameLabel(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ItemListViewController") as! ItemListViewController
_=controller.view
let tableView = controller.tableView
tableView.dataSource = FakeDataSource()
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell
XCTAssertNotNil(cell.titleLabel)
}
}
extension ItemCellTests{
class FakeDataSource: NSObject, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
}
这里是 ItemCell.swift 代码:
import UIKit
class ItemCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
func configCellWithItem(item: ToDoItem){
}
}
与情节提要相关的所有属性都已连接。什么被忽视了?
【问题讨论】: