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
}
}
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.
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.
it is collectionView.collectionViewLayout
but you need to cast it as flow layout
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With