Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 测试自定义TableViewCell的内容_Ios_Swift_Uitableview - Fatal编程技术网

Ios 测试自定义TableViewCell的内容

Ios 测试自定义TableViewCell的内容,ios,swift,uitableview,Ios,Swift,Uitableview,我正在用Swift 2构建一个iOS应用程序,它使用自定义的表视图单元格、附加的标签、图像视图等(让我们调用类CustomTableViewCell)。我已经将类情节提要连接到每个子视图,并为单元分配了一个标识符。我模拟了数据,并尝试运行应用程序来检查单元格是否正确映射,它看起来还可以 问题是,我无法将已出列的单元格视为CustomTableViewCell,以测试其属性的值。当我向下转换从tableView(tableView,cellforrowatinexpath:indexPath)返回

我正在用Swift 2构建一个iOS应用程序,它使用自定义的表视图单元格、附加的标签、图像视图等(让我们调用类
CustomTableViewCell
)。我已经将类情节提要连接到每个子视图,并为单元分配了一个标识符。我模拟了数据,并尝试运行应用程序来检查单元格是否正确映射,它看起来还可以

问题是,我无法将已出列的单元格视为
CustomTableViewCell
,以测试其属性的值。当我向下转换从
tableView(tableView,cellforrowatinexpath:indexPath)
返回的单元格时,所有自定义属性值都变成
nil
,我的测试失败

这是我的密码:

MyViewControllerTests.swift

  func testShouldConfigureTableViewCellToDisplayNotification() {
    // Given
    sut.tableView = TableViewSpy()

    let items = [ <some items to display> ]
    sut.displayedItems = items

    // When
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    let cell = viewController.tableView(viewController.tableView, cellForRowAtIndexPath: indexPath) as! CustomTableViewCell

    // Then
    XCTAssertEqual(cell.detailLabel?.text, "foo", "A properly configured table view cell should display the notification detail")
    XCTAssertEqual(cell.titleLabel?.text, "Bar", "A properly configured table view cell should display the notification title")
    XCTAssertEqual(cell.dateLabel?.text, "15/04/2016", "A properly configured table view cell should display the notification date")
  }
CustomTableViewCell.swift

class CustomTableViewCell: UITableViewCell {
  // MARK: Properties
  @IBOutlet weak var dateLabel: UILabel!
  @IBOutlet weak var detailLabel: UILabel!
  @IBOutlet weak var titleLabel: UILabel!  

  override func awakeFromNib() {
    super.awakeFromNib()
      // Initialization code
  }

  override func setSelected(selected: Bool, animated: Bool) {
      super.setSelected(selected, animated: animated)

      // Configure the view for the selected state
  }

}
试着替换

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "CustomTableViewCell"
    let displayedItem = displayedItems[indexPath.row]
    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomTableViewCell
    if cell == nil {
      cell = CustomTableViewCell(style: .Value1, reuseIdentifier: cellIdentifier)
    }
    cell!.dateLabel?.text = displayedItem.date
    cell!.detailLabel?.text = displayedItem.detail
    cell!.titleLabel?.text = displayedItem.title
    return cell!
  }

如果您在故事板中正确连接了单元格,那么这应该可以工作。否则,请检查您是否为您的手机正确分配了所有IBOutlet

如果出现问题,请检查以下各项:

1) 在故事板中选择您的单元

2) 在右栏中打开Identity Inspector(顶部的第三个选项卡)。确保单元格的类别设置为
CustomTableViewCell

3) 在属性检查器(第4个选项卡)中,确保单元格标识符拼写正确

4) 在Connections inspector(最后一个选项卡)中,指定您定义的单元格的所有IBOutlet。

尝试替换

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "CustomTableViewCell"
    let displayedItem = displayedItems[indexPath.row]
    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomTableViewCell
    if cell == nil {
      cell = CustomTableViewCell(style: .Value1, reuseIdentifier: cellIdentifier)
    }
    cell!.dateLabel?.text = displayedItem.date
    cell!.detailLabel?.text = displayedItem.detail
    cell!.titleLabel?.text = displayedItem.title
    return cell!
  }

如果您在故事板中正确连接了单元格,那么这应该可以工作。否则,请检查您是否为您的手机正确分配了所有IBOutlet

如果出现问题,请检查以下各项:

1) 在故事板中选择您的单元

2) 在右栏中打开Identity Inspector(顶部的第三个选项卡)。确保单元格的类别设置为
CustomTableViewCell

3) 在属性检查器(第4个选项卡)中,确保单元格标识符拼写正确


4) 在Connections inspector(最后一个选项卡)中,指定您定义的单元格的所有IBOutlet。

从代码示例中可以看出,您没有注册要重用的单元格

tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")

从您的代码示例来看,您似乎没有注册要重用的单元

tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")

这使得XCode抛出一些生成错误:
无法强制展开非可选类型“CustomTableViewCell”
的值,因为我在单元格出列时已将其展开。当我从属性绑定中删除所有这些展开时,我在运行测试时遇到一个致命错误:
“在展开可选值时意外发现nil”
my bad!检查更新答案。问题是在单元格退出队列后使用“cell!”:)这意味着您没有在故事板中正确指定单元格。现在,我将更新我的答案,提供您需要检查的其他内容。我已尝试重新分配插座,查找标识符中的拼写错误,并将CustomTableViewCell类重新分配给我的单元格,我不断收到展开可选错误。一个重要的细节是,只有当我尝试使用
cellForRow:atIndexPath
中的单元格作为CustomTableViewCell进行单元测试时,我才会遇到这个问题,但是当我运行应用程序时,tge值被正确映射,这使得XCode抛出一些构建错误:
无法强制非可选类型“CustomTableViewCell”的值展开,因为我在单元格出列时打开了它。当我从属性绑定中删除所有这些展开时,我在运行测试时遇到一个致命错误:
“在展开可选值时意外发现nil”
my bad!检查更新答案。问题是在单元格退出队列后使用“cell!”:)这意味着您没有在故事板中正确指定单元格。现在,我将更新我的答案,提供您需要检查的其他内容。我已尝试重新分配插座,查找标识符中的拼写错误,并将CustomTableViewCell类重新分配给我的单元格,我不断收到展开可选错误。一个重要的细节是,只有当我尝试使用
cellForRow:atIndexPath
中的单元格作为CustomTableViewCell进行单元测试时,我才会遇到这个问题,但当我运行应用程序时,tge值正确映射。据我所知,如果我不使用故事板进行单元格类映射,我应该注册该类。这些技术是互补的吗?哦,对不起。你是对的。如果您使用的是原型单元,则不需要使用此代码。在这种情况下,确保在IB中添加正确的单元格标识符就足够了。通常,您需要使用此方法获取UITableView cell let cell=tableView.CellForRowatineXpath(indexPath),但您直接调用了委托方法,如果表未显示,该方法可以返回空单元格。我只能建议尝试在UITableView委托方法中设置断点,以了解发生了什么错误。或者用你的VC代码发布示例项目。我尝试了你所指出的,并得出结论,当我尝试下调
cellForRow:atIndexPath
的结果时,我的测试失败了。据我所知,如果我不使用故事板进行单元类映射,我应该注册该类。这些技术是互补的吗?哦,对不起。你是对的。如果您使用的是原型单元,则不需要使用此代码。在这种情况下,确保在IB中添加正确的单元格标识符就足够了。通常,您需要使用此方法获取UITableView cell let cell=tableView.CellForRowatineXpath(indexPath),但您直接调用了委托方法,如果表未显示,该方法可以返回空单元格。我只能建议尝试在UITableView委托方法中设置断点,以了解发生了什么错误。或者用你的VC代码发布示例项目。我已经尝试了你指出的内容,并得出结论,当我试图否定
cellForRow:atInd的结果时,我的测试失败了