cellForRowAtIndexPath在单元测试期间为零

时间:2013-05-14 12:55:35

标签: iphone ios objective-c uitableview

我正在尝试编写一个使用tableView的单元测试,但我无法访问单元格。

这总是零:

- (void)testDidEndEditingUpdatesLiftRecords {
    TextViewCell *cell = (TextViewCell *) [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

尽管我在设置

中设置了数据源
- (void)setUp {
    [super setUp];
    source = [[MyDataSource alloc] init];
    tableView = [[UITableView alloc] init];
    [tableView setDataSource:source];

可能是什么

3 个答案:

答案 0 :(得分:4)

正如文档所说,UITableView方法cellForRowAtIndexPath:返回“表示表格单元格的对象,如果单元格不可见或nil已出现,则返回indexPath范围。“因此,如果单元格当前在tableview上不可见,则此方法将返回nil

请注意,不应将此方法与我们在tableView:cellForRowAtIndexPath:中实施的UITableViewDataSource方法混淆。这些名字容易混淆。

答案 1 :(得分:4)

发布here(Swift 3)我不得不调用cellForRow数据源cellForRowAt函数而不是直接从tableView.cellForRow函数访问

//this 
let cell = controller.tableView(controller.tableView, cellForRowAt: IndexPath(row: 0, section: 0))

//instead of this
let cell = controller.tableView.cellForRow(at: IndexPath(row: 0, section: 0))

答案 2 :(得分:-1)

实例化实际UITableViewController,这样可以正常工作。

相关问题