我在 Storyboard 中创建了一个UICollectionViewController。但是我需要访问flowlayout ...如何访问和使用flowlayout? 这是我的 Controller :
使用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
    }
}

最佳答案

它是collectionView.collectionViewLayout,但您需要将其强制转换为流布局

关于ios - 如何快速获取collectionview的布局?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37408906/

10-11 16:21