Tests for custom UITableViewCell, cellForRowAtIndexPath crashes with nil outlets

后端 未结 4 1003
夕颜
夕颜 2021-01-21 22:58

I have a ViewController that contains a tableView. Since I need to keep the code well covered with tests, I need to write a test for [tableView:cellForRowAtIndexPath]

         


        
4条回答
  •  没有蜡笔的小新
    2021-01-21 23:40

    There are a number of things that could be going wrong here. Some possibilities:

    1. tableView itself could be nil.
    2. No cell prototype in the nib or storyboard with the reuse identifier in question.
    3. You haven't registered a cell class with the reuse identifier in question.
    4. Cell allocation fails for some other reason, such as lack of memory.
    5. Some class, such as the table cell class, not included in the test target. (This would usually cause a build error rather than a crash.)
    6. Cell's .xib file not included in the test target.
    7. Relevant objects in .xib aren't connected to the proper outlets.
    8. Your crash is happening because you're trying to allocate a new cell in your tableView:cellForRowAtIndexPath: method. That used to be necessary if no cells were available for reuse, but these days a properly configured table will instantiate new cells as needed, and IIRC instantiating your own can cause a crash.

提交回复
热议问题