【问题标题】:How to make UICollectionView auto layout如何使 UICollectionView 自动布局
【发布时间】:2015-09-28 08:12:59
【问题描述】:

如何使 UICollectionView 自动布局,以便根据项目总数决定某个时刻出现在屏幕上的项目数。我实际上想做这样的布局,如果我有 9 个项目,那么所有 9 个项目都应该同时显示在集合视图上。如果项目是 8 或其他,则项目的大小应该以这样的方式增加,以使它们仍然占用集合视图的整个区域。

我绘制的条件如下:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    var cell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionViewCell

    if((cell.selected) && (indexPath.row%2 == 0))
    {
        cell.backgroundColor = UIColor.whiteColor()
        imageview.image = UIImage( named: a[indexPath.row])
    }
    else if((cell.selected) && (indexPath.row%2 != 0))
    {
        cell.backgroundColor = UIColor.whiteColor()
        imageview.image = UIImage( named: a[indexPath.row])
    }
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    var abc : CGSize!
    if(a.count == 1){
        abc = CGSize(width: self.view.frame.width, height: self.view.frame.height/2)
    }
    else if(a.count == 2) {
            abc = CGSize(width: self.view.frame.width/2, height: self.view.frame.height/2)
    }
    else if (a.count > 2) && (a.count<6){
        abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/2)
    }
    else {
         abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/5)
    }
         return abc
   }

func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool
{
    return true
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return a.count
}

func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

【问题讨论】:

    标签: ios swift autolayout uicollectionview


    【解决方案1】:

    根据您的问题,我了解到您希望 UICollectionView 的单元格尺寸非常大,以便它们覆盖整个屏幕。

    有多种方法可以实现你想要的,但关键是使用 UICollectionViewLayout。

    例如,UICollectionViewFlowLayout 有一个itemSize 属性。

    根据您的内容(对于单元格),您可以尝试计算它们将占用多少空间,然后设置 FlowLayout 的 itemSize 属性。如果所有的单元格都应该是相同的大小,这应该就足够了。

    您也可以使用委托方法collectionView:layout:sizeForItemAtIndexPath: 来设置每个单元格的大小。

    如果这还不够,例如您还想更改单元格的排列,您将需要对 FlowLayout 进行子类化以计算每个项目的大小。看看这个: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html

    最终,您需要首先根据您的内容计算需要多少个单元格。

    【讨论】:

    • 实际上我想让单元格的数量是动态的,就像用户可以上传任意数量的图片一样,根据图片的数量,我的 collectionView 应该显示不同的排列.. 所以我无法确定预先的项目数量,它是完全动态的
    • @SameerVerma,它是动态的,但您需要在布局实际完成之前知道。因此,在您的示例中,一旦用户完成上传他的照片,您就会知道集合视图将有多少个单元格。或者,如果您想在图片之后添加图片(当用户上传它们时),您需要对布局对象进行子类化,以便在您执行 reloadData 时重新设计单元格
    猜你喜欢
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2015-07-07
    • 2015-08-19
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多