Swift 当内容太长时,具有.estimated width的合成布局项会使应用程序崩溃
我正在探索使用合成布局的奇妙世界,我发现了一个需要帮助的小情况。下面是一个简单的应用程序,它使用CollectionView和CL,使用Swift 当内容太长时,具有.estimated width的合成布局项会使应用程序崩溃,swift,uicollectionviewcell,uicollectionviewlayout,uicollectionviewcompositionallayout,Swift,Uicollectionviewcell,Uicollectionviewlayout,Uicollectionviewcompositionallayout,我正在探索使用合成布局的奇妙世界,我发现了一个需要帮助的小情况。下面是一个简单的应用程序,它使用CollectionView和CL,使用UIListContentConfigurationrounded单元格显示随机字符串。项目的宽度是。估计(30),因此我可以根据单元格的内容获得自调整大小的单元格(宽度)。在我将字符数从50个增加到100个之前,这是非常有效的。(目前在iPad Pro 9.7上运行)。似乎有100个字符超出了CollectionView的宽度,这使得我的应用程序开始使用大量内
UIListContentConfiguration
rounded单元格显示随机字符串。项目的宽度是。估计(30)
,因此我可以根据单元格的内容获得自调整大小的单元格(宽度)。在我将字符数从50个增加到100个之前,这是非常有效的。(目前在iPad Pro 9.7上运行)。似乎有100个字符超出了CollectionView的宽度,这使得我的应用程序开始使用大量内存,直到它因为同样的原因崩溃
如何复制:
将要显示的字符数更改为更大的数字。前任。
return(1…100).compactMap{$0;return self.randomString(长度:.random(in:1..Int{
返回值.count
}
重写func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
var contentConfiguration=UIListContentConfiguration.valueCell()
contentConfiguration.text=值[indexPath.item]
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:“cell”,for:indexath)
cell.contentConfiguration=contentConfiguration
cell.backgroundColor=UIColor(红色:。随机(in:0…1)、绿色:。随机(in:0…1)、蓝色:。随机(in:0…1)、alpha:1)
cell.layer.cornerRadius=cell.frame.height/2
返回单元
}
}
在我看来,问题不在于估计大小的用户,而在于UIListContentConfiguration的使用。因为您使用的是UIListContentConfiguration,所以无法限制标签可以增长的宽度。您的评论给了我一些想法,包括我可以将标签的宽度限制限制为小于两个poi如果标签intrinsicContentSize.width大于collectionView.frame.width,则会比collection.frame.width大。谢谢@mattYes,这是显而易见的,但我不知道如何将自己限制在UIListContentConfiguration中,因为您无法从该方向直接访问标签。这就是我的观点。
import UIKit
class ViewController: UICollectionViewController {
private let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
private lazy var values : [String] = {
return (1...100).compactMap { $0; return self.randomString(length: .random(in: 1..<50))}
}()
private func randomString(length: Int) -> String {
return String((0..<length).map{ _ in letters.randomElement()! })
}
var compositionalLayout : UICollectionViewCompositionalLayout = {
let inset: CGFloat = 2
//Item
let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(30), heightDimension: .fractionalHeight(1))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// Group
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.interItemSpacing = .fixed(4)
group.edgeSpacing = NSCollectionLayoutEdgeSpacing(leading: .fixed(4), top: .fixed(4), trailing: .fixed(0), bottom: .fixed(0))
// Section
let section = NSCollectionLayoutSection(group: group)
return UICollectionViewCompositionalLayout(section: section)
}()
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
}
private func configureUI() {
self.collectionView.collectionViewLayout = compositionalLayout
self.collectionView.dataSource = self
self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
}
extension ViewController {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return values.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var contentConfiguration = UIListContentConfiguration.valueCell()
contentConfiguration.text = values[indexPath.item]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.contentConfiguration = contentConfiguration
cell.backgroundColor = UIColor(red: .random(in: 0...1) , green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1)
cell.layer.cornerRadius = cell.frame.height / 2
return cell
}
}