UIcollectionView间距

时间:2016-08-25 05:09:41

标签: ios swift uicollectionview uicollectionviewlayout

我有10个单元格,其中第6个单元格的宽度必须与其他单元格不同。我试图在flow delegate方法中更改它。但从第7个细胞到第10个细胞的间距出现了问题。

smry=d3.nest()
.key( function(d) { return d.dayofweek;}) 
.rollup(function(d) {return d3.mean(d, function(g) {return g.devcount; })})        
.entries(result);

enter image description here

2 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改,如下所示:

 import UIKit

    class ViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate  {

        @IBOutlet weak var sampleCollectionView: UICollectionView!

        let reuseIdentifier = "cell"

        let insetValue: CGFloat = 5.0

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }

        // make a cell for each cell index path
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

            // get a reference to our storyboard cell
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
            // Use the outlet in our custom class to get a reference to the UILabel in the cell

            return cell
        }

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

            if indexPath.item == 6{
                return CGSizeMake(collectionView.bounds.width - insetValue * 2,100)
            }else{
                return CGSizeMake(floor((collectionView.bounds.width - insetValue * 4)/3), 100)
            }
        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {

            return insetValue;
        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
            return insetValue;
        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{

            return UIEdgeInsetsMake(insetValue, insetValue, insetValue, insetValue);
        }


    }

最终输出:

enter image description here

要下载示例项目,请使用以下链接:

https://github.com/k-sathireddy/SampleCollectionView

答案 1 :(得分:0)

对于小物品的宽度,您应该返回ngModelChange,左侧插入为5,右侧插入为5,间距为30 * 30。 对于大项目的宽度,您应返回ngModel,左侧为5,右侧插入为5 = 10。

您可以替换此

(collectionView.bounds.width - 30)/3

使用

collectionView.bounds.width - 10

*使用floor()来舍入大小数