【问题标题】:visibleCells returns all cells in a collectionView in a tableViewCellvisibleCells 在 tableViewCell 中返回 collectionView 中的所有单元格
【发布时间】:2018-10-17 21:27:27
【问题描述】:

我创建了一个包含collectionView 的custom UITableViewCell。此自定义单元格符合 UICollectionViewDelegate 和 UICollectionViewDataSource 协议,并管理 collectionView。

但是当我在 tableView 中使用这个自定义单元格时,即使屏幕上只有 3 个单元格可见,我的 collectionView 的 visibleCells 属性也会返回所有 20 个单元格。

这会导致我的 collectionView 不重用单元格,因为它认为所有单元格在屏幕上都是可见的并导致内存问题。

这是我在 tableViewController 中的实现:

override func viewDidLoad() {
    super.viewDidLoad()

    guard let bundle = Bundle(identifier: "org.cocoapods.*****") else { return }
    tableView.register(UINib(nibName: "CampaignCollectionViewContainerCell", bundle: bundle), forCellReuseIdentifier: "campaignContainerCellIdentifier")
}

.
.
.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "campaignContainerCellIdentifier") as? CampaignCollectionViewContainerCell else { fatalError() }
    return cell
}

我尝试更改 visibleCells 属性,但它是 get-only。

我想没关系,但我的自定义 UITableViewCell 位于私有 pod 中,而 UITableViewController 位于使用此 pod 的主项目中。

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    您返回的数据是否正确 numberOfSectionsInCollectionViewnumberOfItemsInSection

    【讨论】:

    • 是的,我正在返回我的数据源的计数。
    【解决方案2】:

    我在将集合视图嵌入表格视图单元格之前遇到过这个问题,这是我检查特定单元格是否对用户可见的解决方案

    这个解决方案非常适合这种情况(嵌入式集合/表格)

    注意:您可以创建名为“visibleCells”的数组,并在所有单元格上循环填充它,并将可见的一个添加到该数组中:

    let cellRect = YourCell.contentView.convert(YourCell.contentView.bounds, to: UIScreen.main.coordinateSpace)
    if UIScreen.main.bounds.intersects(cellRect) {
         print("This cell is visible")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多