3

在 UI 测试中,我可以使用以下代码获取第一个单元格:

let app = XCUIApplication()

app.launch()

let tablesQuery = app.tables

let cell = tablesQuery.children(matching:.any).element(boundBy: 0)

如何检查该单元格是否包含 imageview ?

4

4 回答 4

2
public func hasImageViewInside(_ cell: UITableViewCell) -> Bool {
    for child in cell.subviews {
        if let _ = child as? UIImageView {
            return true
        }
    }
    return false
}
于 2017-01-24T12:33:28.527 回答
1

斯威夫特 5

首先在故事板中为单元格的 ImageView 提供可访问性标识符或ViewDidLoad

故事板辅助功能 ID

func testIsImageViewNil() {
    let imageView = app.images["PhotosCollectionViewController.ImageCell.ImageView"]
    XCTAssertNotNil(imageView)
}
于 2020-02-20T23:30:08.837 回答
1
        for viw in cell.contentView.subviews {
        if ((viw as? UIImageView) != nil) {
            print("123")
        }
    }
于 2017-01-24T12:58:08.783 回答
0
  for case let imageView as UIImageView in cell.contentView.subviews  {
      if imageView.tag == 1001 {
         imageView.image = UIImage(named: "myCustomImage")
      }
  }

  //OR Altervnatively


  cell.contentView.subviews.flatMap { $0 as? UIImageView }.forEach { imageView in
      if imageView.tag == 1001 {
         imageView.image = UIImage(named: "myCustomImage")
      }
  }
于 2017-01-24T13:56:14.940 回答