![](/img/trans.png)
[英]Click on collection view cell inside a table view cell in tvos application
[英]How change focus in reused table view cell with nested collection view in tvOS?
我有嵌套集合视图(水平滚动)到表格视图单元格的表格视图。 类似的东西:
-table view:
- section
- cell:
- collection view
在起始屏幕上,我有 3 个部分,每个部分有 1 个单元格。 当我尝试对单元格重复使用 1 个部分时,焦点项目的indexPath不会改变。
例如,如果我在集合视图中选择 1 个第 1 个单元格并聚焦第 12 个项目,那么我以这种方式向下滚动表格视图,我更改聚焦的项目并切换到表格视图的另一个部分。 所以我重用单元格并看到相同的焦点索引路径( 12 行)。 所以集合视图滚动到这个项目。
我试图重启preferredFocusEnvironments在
覆盖 func prepareForReuse()
但没有结果。 当我设置为 false collectionView.remembersLastFocusedIndexPath = false
它并没有帮助我解决问题。 我该如何解决这个问题?
我发现问题出在 collectionView 的 contentOffset 中。 当我重用 tableViewCell 时,我没有在 collectionView 中重置偏移量。 所以我在tableViewCell中保存了每个collectionView的所有偏移量,并通过key将这个值设置为collectionView。 像这样(在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
):
return tableView.dequeueReusableCell(cellClass: HomeTableViewCell.self, for: indexPath).adjust {
if let id = $0.feedID {
contentOffsets[id] = $0.collectionView.contentOffset
}
$0.preprareForDisplay(feed: feeds[indexPath.section)
$0.collectionView.contentOffset = contentOffsets[feeds[indexPath.section].origin.id] ?? .zero
}
所以在那之后重点工作正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.