具有UICollectionViewCell的可重用UITableViewCell

时间:2018-09-20 10:53:02

标签: ios swift uitableview uicollectionview

代码:

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的{​​{1}}并显示新的UITableViewCell且具有此自定义流布局的单元格被重用并且UICollectionViewFlowLayout已经具有UICollectionView时,不好。

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

也许是UICollectionViewFlowLayout的东西?

我补充说,prepareForReuse()UICollectionView中的出口。

1 个答案:

答案 0 :(得分:0)

这篇出色的文章帮助我在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

相关问题