我正在尝试UICollectionViewCells使用自动布局进行自我调整,但我似乎无法让单元格根据内容调整自己的大小.我无法理解如何根据单元格内容中的内容更新单元格的大小.
这是我尝试过的设置:
UICollectionViewCell一个UITextView.UITextView已禁用.UITextView固定在单元格左侧,显式宽度为320.UITextView固定到单元格的顶部.UITextView具有高度限制设置为其中一个恒定的UITextView报告将适合文本.这是单元格背景设置为红色,UITextView背景设置为蓝色的样子:

我把我已经在GitHub上玩的项目在这里.
我需要显示一堆具有不同高度的collectionViewCell.视图太复杂,我不想手动计算预期的高度.我想强制执行自动布局来计算单元格高度
dequeueReusableCellWithReuseIdentifier在cellForItemAtIndexPathbreak 之外调用collectionView并导致它崩溃
另一个问题是单元格不在单独的xib中,因此我无法手动实例化临时单元并将其用于高度计算.
对此有何解决方案?
public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as UICollectionViewCell
configureCell(cell, item: items[indexPath.row])
cell.contentView.setNeedsLayout()
cell.contentView.layoutIfNeeded()
return cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
}
Run Code Online (Sandbox Code Playgroud)
编辑:
一旦调用dequeueReusableCellWithReuseIdentifier,就会发生崩溃.如果我不调用该方法而是返回一个大小,一切都很好,单元格显示没有计算的大小
流布局中不支持负值或零大小
2015-01-26 18:24:34.231 [13383:9752256] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x00000001095aef35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000109243bb7 objc_exception_throw + 45 …Run Code Online (Sandbox Code Playgroud) 我试图创建一个集合视图,单元格显示可变长度的字符串.
我使用此功能来设置单元格布局:
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
var cellSize:CGSize = CGSizeMake(self.whyCollectionView.frame.width, 86)
return cellSize
}
Run Code Online (Sandbox Code Playgroud)
我想做的是cellSize.height根据我的cell.labelString.utf16Count长度操纵.基本的逻辑就是那个
if((cell.labelString.text) > 70){
cellSize.height = x
}
else{
cellSize.height = y
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法设法检索始终返回nil的单元格标签字符串长度.(我认为它尚未加载......
为了更好地理解,这里是完整的代码:
// WhyCell section
var whyData:NSMutableArray! = NSMutableArray()
var textLength:Int!
@IBOutlet weak var whyCollectionView: UICollectionView!
//Loading data
@IBAction func loadData() {
whyData.removeAllObjects()
var findWhyData:PFQuery = PFQuery(className: "PlacesWhy")
findWhyData.whereKey("placeName", equalTo: placeName)
findWhyData.findObjectsInBackgroundWithBlock({
(objects:[AnyObject]!,error:NSError!)->Void in
if (error == nil) {
for object in objects { …Run Code Online (Sandbox Code Playgroud) 假设您有一个带有普通自定义 UICollectionViewLayout 的 UICollectionView。
所以这不是<<<流布局 - 这是一个正常的自定义布局。
自定义布局很简单,在prepare调用中您只需遍历数据并布置每个矩形即可。所以说这是一个垂直滚动集合......
override func prepare() {
cache = []
var y: CGFloat = 0
let k = collectionView?.numberOfItems(inSection: 0) ?? 0
// or indeed, just get that direct from your data
for i in 0 ..< k {
// say you have three cell types ...
let h = ... depending on the cell type, say 100, 200 or 300
let f = CGRect(
origin: CGPoint(x: 0, y: y ), …Run Code Online (Sandbox Code Playgroud) TLDR;根据部分,我需要调整单元格的大小:
我只是在寻找:“是的,您可以通过组合布局来实现这一点,这就是如何......”。或者“不,你不能用组合来实现这个,你必须使用流布局或其他一些自定义布局。”
几个额外的要求:
UITableViewAutomaticDimension背景资料:
dynamic ios uicollectionview swift uicollectionviewcompositionallayout
在/sf/answers/3586231701/中,它展示了如何UICollectionView通过使用在 Vertical 的单元格中实现全宽度、动态高度UICollectionViewCompositionalLayout
我们希望在水平上实现同样的目标UICollectionView,但有要求
我们的解决方案如下所示
class MenuTabsView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collView: UICollectionView = {
let itemSize = NSCollectionLayoutSize(
widthDimension: NSCollectionLayoutDimension.estimated(44),
heightDimension: NSCollectionLayoutDimension.fractionalHeight(1.0)
)
let item = NSCollectionLayoutItem(
layoutSize: itemSize
)
item.contentInsets = NSDirectionalEdgeInsets(
top: 0,
leading: 0,
bottom: 0,
trailing: 1
)
let group = NSCollectionLayoutGroup.horizontal(
layoutSize: itemSize,
subitem: item,
count: 1
)
let section = NSCollectionLayoutSection(group: group)
let configuration = UICollectionViewCompositionalLayoutConfiguration()
configuration.scrollDirection = .horizontal …Run Code Online (Sandbox Code Playgroud)