[英]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.