我对UICollectionView有一个问题。我需要使用以下属性创建UICollectionView:
我无法在同一时间实现2和4。只需着色单元格(无需在单元格中放置任何对象)。
我需要Swift中的解决方案。
以下是截屏http://i.imgur.com/ovmdyla.png和我的代码:
let imagesNames = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
override func viewDidLoad() {
super.viewDidLoad()
setUpUI()
}
func setUpUI(){
let flowLayoutForCV = UICollectionViewFlowLayout()
flowLayoutForCV.sectionInset = UIEdgeInsets(top: 10, left: UIScreen.mainScreen().bounds.width * 0.03, bottom: 10, right: UIScreen.mainScreen().bounds.width * 0.03)
let cV = UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height), collectionViewLayout: flowLayoutForCV)
cV.backgroundColor = UIColor.whiteColor()
cV.dataSource = self
cV.delegate = self
cV.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
self.view.addSubview(cV)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesNames.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellID", forIndexPath: indexPath)
let red: CGFloat = CGFloat(arc4random_uniform(255)) / 255.0
let green: CGFloat = CGFloat(arc4random_uniform(255)) / 255.0
let blue: CGFloat = CGFloat(arc4random_uniform(255)) / 255.0
cell.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: UIScreen.mainScreen().bounds.width * 0.455, height: CGFloat(arc4random_uniform(250)) + 50)
}