UICollectionView
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let screenRect = UIScreen.main.bounds
let rect = CGRect(x: 0, y: 20, width: screenRect.size.width, height: screenRect.size.height / 3)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 200, height: 200)
layout.minimumLineSpacing = 30
layout.minimumInteritemSpacing = 10
let collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.white
collectionView.register(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "reusedCell")
collectionView.delegate = self
collectionView.dataSource = self
self.view.addSubview(collectionView)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reusedCell", for: indexPath)
cell.backgroundColor = UIColor.blue
return cell
}
}
网友评论