【问题标题】:How to set dynamic UICollectionView cell height?如何设置动态 UICollectionView 单元格高度?
【发布时间】:2016-01-28 10:25:18
【问题描述】:

如何设置动态单元格高度?

我需要在这个方法中设置单元格大小:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 

我尝试这样做:

cell.bounds = CGRect(x: cell.bounds.origin.x, y: cell.bounds.origin.y, width: width, height: height) 

但是我不能向下滚动...

我试试这个方法:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 

但是这个方法是先调用,再调用这个方法:

cell.bounds = CGRect(x: cell.bounds.origin.x, y: cell.bounds.origin.y, width: width, height: height) 

那么..如何更改单元格高度?

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

    首先,正常设置。 (UICollectionView 的法线约束 [left、right、top、bottom 和 height] 和 IBOutlet。

    其次,为高度约束、顶部约束和底部约束制作IBOutlets。

    第三,添加->

    //these are the outlets I am naming generic names but name them what you wish
    
     IBOutlet var collectionViewCellDynamicConstraint: NSLayoutConstraint!
     IBOutlet var topConstraintDynamicConstraint: NSLayoutConstraint!
     IBOutlet var bottomConstraintDynamicConstraint: NSLayoutConstraint!
    
     //into viewDidLoad
    
     collectionViewCell.addObserver(self, forKeyPath: "contentHeight",      options:NSKeyValueObservingOptions.New, context: nil)
    
      topConstraintDynamicConstraint.addObserver(self, forKeyPath: "contentTop", options:NSKeyValueObservingOptions.New, context: nil)
    
      bottomConstraintDynamicConstraint.addObserver(self, forKeyPath: "contentBottom", options:NSKeyValueObservingOptions.New, context: nil)
    
     //create this override func
    
     override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    
    
    
     //try use this segment below as well but if it doesn't work then remove it and try run again
    
     self.collectionViewCell.sizeThatFits()
    
     //
    
    
    
     self.collectionViewCellDynamicConstraint.constant = collectionViewCell.sizeThatFits(CGSizeMake(self.collectionViewCell.frame.size.width, CGFloat.max)).height
    
     // set these values (below) to the space/value that you set them at normally. Often what is needed is a refresh after changing the height of the view/collectionViewCell
    
        self.topSpaceOld.constant = 0.4 
    
        self.bottomSpaceOld.constant = 10
    
    
    }
    
    
    
    
     // you need to de-initialise them before you leave the view or else it will crash
    deinit {
        self.collectionViewCell.removeObserver(self, forKeyPath: "contentHeight")
    
        self.topConstraintDynamicConstraint.removeObserver(self, forKeyPath: "contentTop"
    
        self.bottomConstraintDynamicConstraint.removeObserver(self, forKeyPath: "contentBottom")
     )
    
    
    
    }
    

    可能存在一些命名问题,但应该可以。此代码用于使 UITextView 在滚动视图内工作以获取视图的动态高度。应该是同一个概念。

    【讨论】:

    • 嗨,这个答案对你有帮助吗?如果它有效,请验证它是正确的答案
    【解决方案2】:

    您可以使用 sizeForItemAtIndexPath 委托方法调整单元格的大小,如下所示。您需要为单元格返回 CGSize。您能否向我们展示您实现此方法的完整代码?

    工作示例;

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        return CGSizeMake(width, height);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多