有没有办法在表视图中获取单元格数?

时间:2016-10-19 10:17:52

标签: earlgrey

如果我们要将TableView中的所有单元格接收到一个数组中并迭代它以单击元素。我正在寻找一个快速的解决方案。

2 个答案:

答案 0 :(得分:1)

如果每个单元格都有一个区分因子,那么你可以得到一个数组。 要使用文本字符串,我必须先获取单元格。因此,这导致 - 如何获取单元格的计数,并在获取单元格之后,向下钻取以查看这是否是文本,然后打开上下文菜单,否则执行某些操作。

这是你可以做的:继续在单元格上使用'atIndex:'。使用selectElementWithMatcher :: withError。循环直到找到indexOutOfBoundsError然后你应该有文本。

但是使用atIndex:循环时,你应该拥有你想要的单元格。要做同样的事情,请参见下文:

for (int i = 0; i < someLargeValue; i++) {
 EarlGrey.selectElementWithMatcher(grey_accessibilityID("abc")).atIndex(i)
}

- &GT;好的,为了获得“someLargeValue”的值,请使用selectElementWithMatcher :: withError。循环直到找到indexOutOfBoundsError然后你应该有文本。

答案 1 :(得分:0)

这可以通过为GREYAssertionBlock实现自定义UITableView来实现:

func assertTableView(_ accessibilityID: String, hasRowCount rowCount: Int, inSection section: Int) {
    let cellCountAssert = GREYAssertionBlock(name: "cell count") { (element, error) -> Bool in
        guard let tableView = element as? UITableView, tableView.numberOfSections > section else {
            return false
        }
        let numberOfCells = tableView.numberOfRows(inSection: section)
        return numberOfCells == rowCount
    }

    EarlGrey.selectElement(with: grey_accessibilityID(accessibilityID)).assert(cellCountAssert)
}