简体   繁体   English

如何在 tvOS 中使用嵌套集合视图更改重用表视图单元格的焦点?

[英]How change focus in reused table view cell with nested collection view in tvOS?

I have table view with nested collection view(horizontal scrolling) into table view cell.我有嵌套集合视图(水平滚动)到表格视图单元格的表格视图。 Something like that:类似的东西:

-table view: 
  - section
   - cell: 
    - collection view

On starting screen I have 3 sections with 1 cell in each section.在起始屏幕上,我有 3 个部分,每个部分有 1 个单元格。 When I try to reuse 1 section with cell, indexPath for focused item don't change.当我尝试对单元格重复使用 1 个部分时,焦点项目的indexPath不会改变。

For example, if I select 1 section 1 cell and focused 12th item in collection view, then I scroll down table view in this manner I change focused item and switch to another section of table view.例如,如果我在集合视图中选择 1 个第 1 个单元格并聚焦第 12 个项目,那么我以这种方式向下滚动表格视图,我更改聚焦的项目并切换到表格视图的另一个部分。 So I reuse cell and see the same focused indexPath ( 12 row ).所以我重用单元格并看到相同的焦点索引路径12 行)。 So collection view scrolles to this item.所以集合视图滚动到这个项目。

I tried to reset preferredFocusEnvironments in我试图重启preferredFocusEnvironments

override func prepareForReuse()覆盖 func prepareForReuse()

but it had no result.但没有结果。 And when I set to false collectionView.remembersLastFocusedIndexPath = false it didn't help me to solve the problem.当我设置为 false collectionView.remembersLastFocusedIndexPath = false它并没有帮助我解决问题。 How can I fix this problem?我该如何解决这个问题?

I found out that problem was in contentOffset in collectionView .我发现问题出在 collectionView 的 contentOffset 中 When I reused tableViewCell I didn't reset offset in collectionView.当我重用 tableViewCell 时,我没有在 collectionView 中重置偏移量。 So I saved all offset for each collectionView in tableViewCell and set this value by key to collectionView.所以我在tableViewCell中保存了每个collectionView的所有偏移量,并通过key将这个值设置为collectionView。 Something like this(in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ):像这样(在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
  }

So after that focus work correct.所以在那之后重点工作正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在tvos应用程序的表视图单元格中单击集合视图单元格 - Click on collection view cell inside a table view cell in tvos application 如何找出(使用 XCTest UITest)在 tvOS AppleTV 的表格视图中具有焦点的单元格? - How to find out the (using XCTest UITest) a cell that has a focus in a table view in tvOS AppleTV? 如何在集合视图单元格中更改图像视图 - How to change Image view in collection view cell 直到重新使用单元格,集合视图单元格子视图框架才可用 - Collection View Cell subview frame not available until cell is reused iOS Swift:自动调整大小的集合视图嵌套在表格视图单元格中 - iOS swift: autoresize collection view nested inside a table view cell 如何聚焦集合视图的可见单元格的最后一个单元格? - How to Focus last cell of visible cell of collection view? Swift:永远不会调用集合视图单元中 UIControl 的 tvOS IBAction - Swift: tvOS IBAction for UIControl in Collection View cell never gets called 滚动后重复使用单元格后如何在表格视图单元格中保持值? - how to maintain the value in table view cell after the cell is reused after scrolling? 如何设置细分控件以更改表视图单元格中的集合单元格项目? - How to set a segment control to change collection cell item in table view cell? 焦点引擎在集合视图单元上不起作用 - Focus engine not working on collection view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM