Jacob Smith Jacob Smith - 1 year ago 214
Swift Question

How to get flowlayout of collectionview in swift?

I created a

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() {

// 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() {
// 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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download