在无效的indexPath处捕获了对rect的NSInternalInconsistencyException请求

Cos*_*ows 6 xcode ios xctest swift

以下行崩溃了该程序:

let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell
Run Code Online (Sandbox Code Playgroud)

控制台日志显示以下错误:

"caught "NSInternalInconsistencyException", "request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})"
Run Code Online (Sandbox Code Playgroud)

这是我检查过的一些项目清单:

  • 身份检查器中列出了正确的故事板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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是ItemCell.swift代码:

import UIKit

class ItemCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!


    func configCellWithItem(item: ToDoItem){

    }
}
Run Code Online (Sandbox Code Playgroud)

绑定到故事板的所有属性都已连接.什么被忽视了?

das*_*dom 13

看起来这段代码来自我的书.这是本书中的一个已知错误.将测试方法更改为以下内容:

func testSUT_HasNameLabel(){

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewControllerWithIdentifier("ItemListViewController") as! ItemListViewController

    _=controller.view

    let tableView = controller.tableView
    let dataProvider = FakeDataSource()
    tableView.dataSource = dataProvider

    let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell

    XCTAssertNotNil(cell.titleLabel)
}
Run Code Online (Sandbox Code Playgroud)

然后,当您稍后重构该代码以将设置放入setUp方法时,将属性添加let dataProvider = FakeDataSource()到测试用例并将其设置为表视图(tableView.dataSource = dataProvider)的数据源setUp.

  • 本书中的版本不起作用的原因是`tableView`的`dataSource`是一个弱引用.使用新实例进行设置时,会立即释放它.因此,您需要该对象的局部变量.当我写这本书时,我没有看到,因为我在iPhone 4s模拟器上测试了代码并且它没有崩溃.我不知道为什么. (2认同)