Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
- let cell = "cellId"
- let text = UILabel()
- override func viewDidLoad() {
- super.viewDidLoad()
- navigationItem.title = "Vote"
- collectionView?.backgroundColor = UIColor.white
- collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cell)
- }
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 5
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, for: indexPath)
- cell.backgroundColor = UIColor.lightGray
- return cell
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width: view.frame.width, height: 200)
- }
- }
- class MyCell : UICollectionViewCell {
- var label1: UILabel
- var label2: UILabel
- var bgImg: UIImageView
- }
- collectionView?.register(MyCell.self, forCellWithReuseIdentifier: cell)
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell,
- for: indexPath) as! MyCell
- cell.bgImg.image = UIImage(named: "img.png")
- cell.label1.text = "..."
- cell.label2.text = "..."
- return cell
- class CustomCollectionViewCell: UICollectionViewCell {
- override init(frame: CGRect) {
- super.init(frame: frame)
- let imageView = UIImageView(frame: self.bounds)
- //customise imageview
- imageView.backgroundColor = UIColor.red
- contentView.addSubview(imageView)
- let label = UILabel(frame: CGRect(x: 20, y: 20, width: self.bounds.width - 20, height: 20))
- //Customsize label
- label.text = "Hello"
- label.textColor = UIColor.white
- contentView.addSubview(label)
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override var bounds: CGRect {
- didSet {
- contentView.frame = bounds
- }
- }
- }
Add Comment
Please, Sign In to add comment