0

Code:

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
}

Passing viewModel to Cell:

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()

    }
}

Description:

I have six UITableViewCell mostly, and they are reusable.

In every UITableViewCell is UICollectionView.

Five UICollectionView's use normal UICollectionViewFlowLayout's, but one needs a custom subclass.

The problem is that when UITableViewCell with custom UICollectionViewFlowLayout is hiding and new UITableViewCell is showing and cell with this custom flow layout is reused and UICollectionView already have UICollectionViewFlowLayout but is bad.

Is any nice way to clear this layout or prevent this situation?

Maybe something with prepareForReuse()?

I add that UICollectionView is outlet in UITableViewCell.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Amdx Rux
  • 95
  • 8
  • Can you post an image of what you are trying to achieve? – Sahil Manchanda Sep 20 '18 at 11:06
  • Custom UICollectionViewFlowLayouts means items in 2 rows close to each other. Normal is just horizontal collection with good spacing and this custom is reused in row where should be normal flow layout. just it – Amdx Rux Sep 20 '18 at 11:17

1 Answers1

0

This excellent article helped me a lot to get UICollectionViews in UITableviewCells up and running: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

To update the layout you can call

collectionView.collectionViewLayout.invalidateLayout()

See also: Swift: How to refresh UICollectionView layout after rotation of the device

qRis
  • 86
  • 6
  • I tried invalidateLayout() in PrepareForReuse but it does not work for me – Amdx Rux Sep 20 '18 at 11:17
  • Where do you set the different layouts to the collectionview? You need to call invalidateLayout() after that. PrepareForReuse is probably too early as it's called before cellForRow so the collectionView does not know anything about the new layout. Try calling invalidateLayout() in the viewModel didSet of your cell. – qRis Sep 20 '18 at 11:25
  • new layout is created in collectionViewModel.setup(collectionView: collectionView) in didSet. – Amdx Rux Sep 20 '18 at 11:27
  • Optional() DLA -> Backstage COLLECTIONVIEW -> 0x00007fb8a1174400 TABLEVIEW -> 0x00007fb8a117e200 Optional() DLA -> Recommendations COLLECTIONVIEW -> 0x00007fb8a1174400 TABLEVIEW -> 0x00007fb8a117e200 I tried at top of didSet but nothing. You can see that cell from Backstage is reuse for Recommendation and the FlowLayout is the same but should not be... – Amdx Rux Sep 20 '18 at 11:31
  • Try the end of didSet or within collectionViewModel.setup(collectionView: collectionView) after the new layout is set to the collectionView – qRis Sep 20 '18 at 11:55
  • Did it work? If so, please mark answer as correct. Thx. – qRis Sep 24 '18 at 10:14