UITableView.reload将旧单元格留在TableView中但隐藏了

时间:2019-01-02 23:10:08

标签: ios swift uitableview earlgrey

我有一个用于显示搜索结果的UITableView。键入时,我正在呼叫Tableview.reloadData()。在视觉上,一切正常。当我开始输入文字时,我最多显示5个匹配项,而当我输入以下内容时,该列表将正确显示较少的项目。这是单元格的创建方式和报告的行数。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceCell
    if shouldShowSearchResults {
        let place = filteredPlaces[indexPath.row]
        cell.dataSource = place
    } else {
        let place = allPlaces[indexPath.row]
        cell.dataSource = place
    }
    cell.delegate = self
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if shouldShowSearchResults {
        vlog?.debug("Number of FILTERED rows in PlacesTableView: \(filteredPlaces.count)")
        return filteredPlaces.count
    } else {
        vlog?.debug("Number of unfiltered rows in PlacesTableView: \(allPlaces.count)")
        return allPlaces.count
    }
}

由于PlaceCell是自定义类,因此以下是其中的一些详细信息:

// I've omitted labels, etc.
class PlaceCell: UITableViewCell {

    var dataSource : PlaceView? {
        didSet {
            if let ds = dataSource {
                self.isAccessibilityElement = true
                self.accessibilityLabel = ds.getAccessibilityLabel()

            } else {
                self.isAccessibilityElement = true
                self.accessibilityLabel = nil
            }
        }
    }
    weak var delegate : PlaceCellDelegate? = nil

    override func prepareForReuse() {
        self.isAccessibilityElement = false
        self.accessibilityLabel = nil
        super.prepareForReuse()
    }
}

当使用谷歌的伯爵格雷的UI测试由于多个具有相同辅助功能标签的单元而开始失败时,我开始注意到一个问题。从视觉上看,我不明白为什么会失败,因为只有一个可见的单元格可以匹配。

在使用Reveal检查视图时,似乎随着单元格数量下降到最大值5以下,旧的单元格仍在TableView中,但被隐藏了。因此,有一个隐藏的单元格曾经用来显示与其他单元格所显示的数据相同的数据。

知道为什么会这样吗?这已经工作了好几个月了,我不确定发生了什么变化。

1 个答案:

答案 0 :(得分:1)

遍历视图层次结构时总是很危险;事情可能会改变,也许这就是这里发生的事情。

无论如何,通过使用grey_sufficientlyVisible

仅选择带有所需标签的可见项目,可以使测试更加可靠

类似的东西:

grey_allOf(grey_accessibilityLabel("Whole Foods Market, East Mayo Boulevard, Phoenix"), grey_sufficientlyVisible(), nil)
相关问题