0

I was using a Collection View and setting the Layout to self.collectionView.collectionViewLayout = setupLayout(), where:

func setupLayout() -> UICollectionViewCompositionalLayout {
  let layout = UICollectionViewCompositionalLayout { (sectionNumber, env) -> NSCollectionLayoutSection? in

      let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(77)))
      item.contentInsets.bottom = 5

      let group = NSCollectionLayoutGroup.vertical(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1), subitems: [item])

      let section = NSCollectionLayoutSection(group: group)
      section.contentInsets.leading = 5
      section.contentInsets.trailing = 5

      return section
  }
  return layout
}

However, my boss wants the cell to keep having the characteristics of a TableView, with the best possible performance, containing swipe actions, for example, which are a particularity of UITableView. I saw that there is a way to initialize a compositional layout as a list, using the UICollectionViewCompositionalLayout.list and passing a configuration as:

func setupLayout() -> UICollectionViewCompositionalLayout {
  var config = UICollectionLayoutListConfiguration(appearance: .grouped)
  config.trailingSwipeActionsConfigurationProvider = { indexPath in
       return UISwipeActionsConfiguration(actions: [self.makeRemoveAccessContextualAction(forRowAt: indexPath)])
  }
        
  let layout = UICollectionViewCompositionalLayout.list(using: config)

  return layout
}

The problem is that using it this way, I don't know how to control the margins of items and sections, like: item.contentInsets.bottom and other contentInsets. Is it possible to manipulate cell spacing using UICollectionViewCompositionalLayout.list ?

Update:

It was a TableView, but it needed to have different cell sizes and spacing. I suggested a CollectionView, but it needs to continue with the performance of a TableView and contain the Swipe Actions.

Maykon Meneghel
  • 335
  • 6
  • 8
  • Can’t you just go with a traditional flow layout? – valeCocoa Jun 27 '22 at 21:01
  • There are several other ways to do this, I confess that the easiest was to use a ready-made library that implements swipe actions in a collection view and use the traditional layout, as you suggested, or even the first setupLayout I mentioned. But I wanted to know if it's possible to do it this way, using the CompositionalLayout.list. – Maykon Meneghel Jun 27 '22 at 21:22

0 Answers0