viewController şöyle;
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vCollectionView: UICollectionView!
let content = ["caps1","caps2","caps3","caps4"];
let cellReuseIdentifier:String = "myCell"
override func viewDidLoad() {
super.viewDidLoad()
self.vCollectionView.registerClass(vCollectionViewCell.self, forCellWithReuseIdentifier: self.cellReuseIdentifier)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(vCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.content.count;
}
func collectionView(vCollectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let vCell:vCollectionViewCell = (vCollectionView.dequeueReusableCellWithReuseIdentifier(self.cellReuseIdentifier, forIndexPath: indexPath) as? vCollectionViewCell)!
return vCell;
}
}
vCollectionViewCell'de şöyle olmalı;
class vCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var collectionView: UICollectionView!
let cellReuseIdentifier:String = "myInnerCell"
var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
override init(frame: CGRect) {
super.init(frame: frame)
self.layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
self.layout.itemSize = CGSize(width:self.frame.width-40, height:self.frame.height-100)
self.layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
self.collectionView = UICollectionView(frame:self.frame, collectionViewLayout: self.layout)
self.collectionView.backgroundColor = UIColor.purpleColor()
self.collectionView.frame = self.bounds
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: self.cellReuseIdentifier)
self.collectionView.reloadData()
self.addSubview(self.collectionView)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.cellReuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
cell.backgroundColor = UIColor.orangeColor()
return cell
}
}