UITableView.reload在TableView中保留旧单元格,但将其隐藏



我有一个用于显示搜索结果的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()
}
}

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

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

知道为什么会发生这种事吗?这种方法已经用了好几个月了,我不确定发生了什么变化。

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

无论如何,您可以通过使用grey_sufficientlyVisible仅选择具有所需标签的可见项目来使测试更加稳健

类似于:

grey_allOf(grey_accessibilityLabel("Whole Foods Market, East Mayo Boulevard, Phoenix"), grey_sufficientlyVisible(), nil)

最新更新