如何在Swift中获取CollectionView的FlowLayout?

18

我在Storyboard中创建了一个UICollectionViewController。但是我需要访问流式布局... 我该如何访问和使用流式布局?这是我的控制器:尝试使用collectionView.flowLayout并没有奏效。

class TableCollectionViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    var json: JSON?

    override func viewDidLoad() {
        super.viewDidLoad()


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

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

    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let cellWidth = collectionView.bounds.width/(CGFloat(json!["Rows"][0]["Column"].count)+1)

        return CGSizeMake(cellWidth, 100)
    }


    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return json!["Rows"].count
        //return 5
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return json!["Rows"][section]["Column"].count
        //return 5
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyTableCell", forIndexPath: indexPath) as! MyTableCollectionViewCell
        cell.backgroundColor = UIColor.whiteColor()

        cell.label.text = json!["Rows"][indexPath.section]["Column"][indexPath.row].stringValue
        cell.label.lineBreakMode = .ByWordWrapping
        cell.label.numberOfLines = 0
        //cell.label.text = "deneme"

        // Configure the cell

        return cell
    }
}
3个回答

33

这是collectionView.collectionViewLayout,但你需要将其转换为流式布局。


1
你可以这样进行类型转换: guard let layout = yourCollectionView?.collectionViewLayout as? UICollectionViewFlowLayout else { return } - B-Rad

16

您使用相同的collectionViewLayout变量,需要的UICollectionViewFlowLayout类是UICollectionViewLayout的一个子类:

if let collectionViewFlowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {

    // Use collectionViewFlowLayout

}

4

虽然以上答案适用于生成代码的集合视图,但如果您是通过Interface Builder生成的,则还有另一种方法。

首先,为您的UICollectionViewFlowLayout创建一个outlet:

@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!

然后在Interface Builder中分配它:

输入图像描述

然后使用以下方法:

flowLayout.estimatedItemSize = CGSize(width: 1, height: 1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接