【问题标题】:Collection view cell auto layout deferent hight for each cell集合视图单元格自动布局每个单元格的不同高度
【发布时间】:2016-11-10 13:33:38
【问题描述】:

问题是每个单元格都有不同的 UILabel 内容,我希望单元格的宽度固定,高度灵活,具体取决于 UILabel hight 的内容,我尝试了针对这个问题的不同解决方案并观看了几个教程但是不幸的是,它对我没有锻炼。

**我有一个视图控制器和两个集合视图 我只需要其中一个有一个灵活的高度**

extension TextViewController : UICollectionViewDelegateFlowLayout {

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

    if collectionView == PagesCollectionView
    {
    return CGSizeMake(44.0, 44.0)
    }
    else {

        width = UIScreen.mainScreen().bounds.width - CGFloat( 14 )

        return CGSizeMake( width , hight )
    }


}

编辑 1:

有没有像在表格视图中那样的简单方法?

示例:-

override func viewDidLoad() {

super.viewDidLoad()

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension }

【问题讨论】:

  • 检查此链接可能会对您有所帮助stackoverflow.com/questions/27285258/…
  • Thx,这是一个很好的方法,但不是我需要的,我希望高度等于标签高度。是否可以从这个乐趣中访问 c​​ell.label.hight ?我试图访问它,但不知道该怎么做。

标签: swift autolayout ios9 uicollectionviewcell


【解决方案1】:

我假设你要在单元格中显示一个文本

extension TextViewController : UICollectionViewDelegateFlowLayout {

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

        if collectionView == PagesCollectionView
        {
        return CGSizeMake(44.0, 44.0)
        }
        else {

            let width = UIScreen.mainScreen().bounds.width - CGFloat( 14 )
            let text = YourDataSource[indexPath.item].text
            let size = CGSizeMake(width, 800) // just any arbitrary height to make it compile
            let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
            let estimatedFrame = NSString(string: text).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(16)], context: nil)
            return CGSizeMake(width , estimatedFrame.height)
        }


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 2015-06-18
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多