Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get flowlayout of collectionview in swift?

Tags:

ios

swift

I created a UICollectionViewController in the storyboard. But I need to acccess to flowlayout... How do I access and use the flowlayout? Here is my controller: Using collectionView.flowlayout did not work.

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
    }
}
like image 822
Jacob Smith Avatar asked May 24 '16 09:05

Jacob Smith


People also ask

What is Collectionview flowLayout?

A flow layout is a type of collection view layout. Items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row containing as many cells as will fit. Cells can be the same sizes or different sizes.

What is Collectionview in Swift?

From apple's documentation, UICollectionView is: An object that manages an ordered collection of data items and presents them using customizable layouts. The name and definition makes it clear, it is a way to display a Collection of UI Views in our app.


2 Answers

it is collectionView.collectionViewLayout but you need to cast it as flow layout

like image 197
Mert Buran Avatar answered Sep 20 '22 05:09

Mert Buran


You use the same collectionViewLayout variable, the UICollectionViewFlowLayout class you need is a subclass of UICollectionViewLayout:

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

    // Use collectionViewFlowLayout

}
like image 29
Alex Staravoitau Avatar answered Sep 21 '22 05:09

Alex Staravoitau