我想检查一个单元格(故事板!)是否满足某些要求(比如某些属性等)但我似乎无法从故事板中实例化它。
override func setUp() {
tableView = UITableView()
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
viewController = storyboard.instantiateViewControllerWithIdentifier("tvcStorageList") as? tvcStorageList
let x = viewController.view
navController = MockNavigationController(rootViewController: viewController)
successMockDBDidReplaceProductDB = substituteWithMockDatabase()
viewController.viewDidLoad()
}
func testCorrectTableCellIsReturned() {
viewController.viewDidLoad()
let cell = viewController.tableView(tableView, cellForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: 0))
println(cell)
}
//表......
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("StorageCell") as! Storage2TableViewCell
let row = indexPath.row
let temp = storageList[row]
let thisStorage = temp
cell.name.text = temp.name
尝试测试一个错误的访问权限,其中单元格应该出列。
答案 0 :(得分:3)
我自己找到了解决方案。实际上是相当愚蠢的错误......
我必须
let cell = viewController.tableView(viewController.tableView, cellForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: 0))
答案 1 :(得分:0)
发布[here] [1](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 :(得分:0)
从iOS11开始,如果您尝试在多个单元格中进行迭代,此解决方案可能会触发错误。
Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: cell, index path:
“这是iOS 11中添加的一个新断言,用于防止在UITableView上滥用此API。只应在tableView:cellForRowAtIndexPath内部调用dequeueReusableCellWithIdentifier:forIndexPath:方法:响应来自表视图的回调。 “
大多数客户端点击这个断言是因为他们错误地从其他地方调用了他们自己的tableView:cellForRowAtIndexPath:的实现,而不是表视图调用该方法的正常情况,这导致一个单元格为传递的索引路径出列表格视图没有要求。在大多数情况下,客户端正在执行此操作(错误地)尝试从表视图获取该索引路径的可见单元格,这可以通过在UITableView上直接使用cellForRowAtIndexPath:查询表视图来正确完成。
对于我的测试用例,我现在用tableView.visibleCells
替换那个位。此外,在此目的中,以编程方式在元素cal之间滚动也有帮助。