iOS UICollectionView如何实现单分区自适配cell、其余分区固定尺寸并解决卡顿
卡顿原因
你当前的代码同时开启了全局自动估算尺寸,又在sizeForItemAt代理中给部分分区返回固定尺寸,触发了UICollectionViewFlowLayout的内部循环布局校验逻辑,导致布局计算陷入死循环,CPU占用拉满、UI无响应。
解决方案
方案1:兼容低版本的FlowLayout实现
不需要改动现有布局结构,只要调整两处配置即可:
- 移除
viewDidLoad中全局设置estimatedItemSize的代码 - 新增
estimatedSizeForItemAt代理方法,仅给需要自适配的分区返回估算尺寸
完整修正代码如下:
import UIKit class ViewController: UIViewController { @IBOutlet var collectionView: UICollectionView! @IBOutlet var collectionViewLayout: UICollectionViewFlowLayout! override func viewDidLoad() { super.viewDidLoad() // 移除全局estimatedItemSize配置 collectionViewLayout.scrollDirection = .vertical collectionView.delegate = self collectionView.dataSource = self } } extension ViewController: UICollectionViewDataSource { func numberOfSections(in collectionView: UICollectionView) -> Int { 2 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 5 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // 为简化演示,此处移除了cell准备相关代码 if indexPath.section == 0 { // MySelfSizingCollectionViewCell可通过autolayout自动计算cell高度 return MySelfSizingCollectionViewCell() } else { return UICollectionViewCell() } } } extension ViewController: UICollectionViewDelegateFlowLayout { // 新增:按分区返回对应估算尺寸 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, estimatedSizeForItemAt indexPath: IndexPath) -> CGSize { if indexPath.section == 0 { // 给自适配分区返回一个接近真实值的估算尺寸即可,比如宽度设为屏幕宽度,高度设为预估的最小高度 return CGSize(width: collectionView.bounds.width, height: 44) } else { // 固定尺寸分区和最终返回的size保持一致即可 return CGSize(width: 100, height: 100) } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if indexPath.section == 0 { return UICollectionViewFlowLayout.automaticSize } else { return .init(width: 100.0, height: 100.0) } } }
方案2:iOS 13+ 推荐使用Compositional Layout
如果你的项目最低支持iOS13,用分区式组合布局实现逻辑更清晰,不会出现上述循环计算问题:
// 替换原有FlowLayout,直接初始化Compositional Layout let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in if sectionIndex == 0 { // 自适配分区:设置估算尺寸 let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group) return section } else { // 固定尺寸分区:设置固定尺寸 let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(100), heightDimension: .absolute(100)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(100)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) // 可按需设置item间距、分区内边距等 group.interItemSpacing = .fixed(10) let section = NSCollectionLayoutSection(group: group) return section } } collectionView.collectionViewLayout = layout
内容的提问来源于stack exchange,提问作者Shubham
相关产品推荐
相关产品推荐

