Skip to main content
 首页 » 编程设计

ios之如何从 Storyboard单元测试单元

2024年01月01日74zengkefu

我想检查一个单元格( Storyboard!)是否满足某些要求(比如具有某些属性等)但我似乎无法从 Storyboard实例化它。

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 

尝试测试一个错误的访问权限,单元格应该出队。

请您参考如下方法:

我自己找到了解决方案。实际上是相当愚蠢的错误....

我必须是

let cell = viewController.tableView(viewController.tableView, cellForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: 0))