【问题标题】:Reusable UITableViewCell with UICollectionViewCell带有 UICollectionViewCell 的可重用 UITableViewCell
【发布时间】:2018-09-20 10:53:02
【问题描述】:

代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    registerCells()
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifierForItem(at: indexPath.row), for: indexPath) as? HorizontalCollectionTableViewCell else {
        return UITableViewCell()
    }
    if cellViewModels.count > indexPath.row {
        let viewModel = cellViewModels[indexPath.row]
        cell.viewModel = viewModel
    }
    return cell
}

viewModel 传递给单元:

var viewModel: TitleAccessoryButtonCollectionViewModel? {
    didSet {
        guard let viewModel = viewModel else {
            return
        }
        titleLabel.text = viewModel.title
        if let buttonTitle = viewModel.accessoryButtonModel?.title {
            setAccessoryButtonTitle(buttonTitle)
        }else{
            accessoryButton.hideTitleLabel()
        }

        if let buttonImage = viewModel.accessoryButtonModel?.image {
            accessoryButton.buttonImageView.image = buttonImage
        }
        else {
            accessoryButton.hideImageView()
        }

        sectionContentImage.image = viewModel.sectionContentImage
        titleLabelLeadingConstraint.constant = viewModel.titleLabelLeadingSpacing
        accessoryButton.isHidden = viewModel.hideAccessoryButton
        sectionContentView.isHidden = viewModel.hidePremiumContentView
        let collectionViewModel = viewModel.collectionViewModel
        collectionViewHeight.constant = CGFloat(collectionViewModel.height)
        collectionViewModel.setup(collectionView: collectionView)
        collectionView.delegate = collectionViewModel.delegate
        collectionView.dataSource = collectionViewModel.dataSource
        collectionView.reloadData()

    }
}

说明:

我主要有六个UITableViewCell,它们是可重复使用的。

在每个UITableViewCell 中都是UICollectionView

五个UICollectionView's 使用普通的UICollectionViewFlowLayout's,但一个需要自定义子类。

问题是,当 UITableViewCell 和自定义 UICollectionViewFlowLayout 被隐藏并且新 UITableViewCell 正在显示并且具有此自定义流布局的单元格被重用并且 UICollectionView 已经有 UICollectionViewFlowLayout 但很糟糕时。

有什么好方法可以清除这种布局或防止这种情况发生吗?

也许是prepareForReuse()

我补充说UICollectionViewUITableViewCell 的出口。

【问题讨论】:

  • 你能张贴一张你想要达到的目标的图片吗?
  • 自定义 UICollectionViewFlowLayouts 表示 2 行中的项目彼此靠近。正常只是具有良好间距的水平集合,并且此自定义在应该是正常流布局的行中重复使用。就这样

标签: ios swift uitableview uicollectionview


【解决方案1】:

这篇优秀的文章帮助我在 UITableviewCells 中启动并运行 UICollectionViews:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

要更新布局,您可以调用

collectionView.collectionViewLayout.invalidateLayout()

另请参阅: Swift: How to refresh UICollectionView layout after rotation of the device

【讨论】:

  • 我在 PrepareForReuse 中尝试了 invalidateLayout() 但它对我不起作用
  • 你在哪里为collectionview设置不同的布局?之后您需要调用 invalidateLayout() 。 PrepareForReuse 可能太早了,因为它在 cellForRow 之前被调用,所以 collectionView 对新布局一无所知。尝试在单元格的 viewModel didSet 中调用 invalidateLayout()。
  • 新布局在didSet的collectionViewModel.setup(collectionView:collectionView)中创建。
  • 可选()DLA - >后台的CollectionView - > 0x00007fb8a1174400泰伯维 - > 0x00007fb8a117e200可选()DLA - >推荐的CollectionView - > 0x00007fb8a1174400泰伯维 - > 0x00007fb8a117e200我试图在 didSet 的顶部,但什么也没有。您可以看到 Backstage 中的单元格被重新用于 Recommendation 并且 FlowLayout 是相同的但不应该...
  • 在新布局设置为collectionView后尝试didSet的结尾或collectionViewModel.setup(collectionView:collectionView)内
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多