Guest User

Untitled

a guest
Sep 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  4.  
  5. let cell = "cellId"
  6. let text = UILabel()
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11. navigationItem.title = "Vote"
  12. collectionView?.backgroundColor = UIColor.white
  13. collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cell)
  14.  
  15. }
  16.  
  17. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  18. return 5
  19. }
  20.  
  21. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  22. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, for: indexPath)
  23. cell.backgroundColor = UIColor.lightGray
  24. return cell
  25. }
  26.  
  27. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  28. return CGSize(width: view.frame.width, height: 200)
  29. }
  30. }
  31.  
  32. class MyCell : UICollectionViewCell {
  33. var label1: UILabel
  34. var label2: UILabel
  35. var bgImg: UIImageView
  36. }
  37.  
  38. collectionView?.register(MyCell.self, forCellWithReuseIdentifier: cell)
  39.  
  40. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell,
  41. for: indexPath) as! MyCell
  42.  
  43. cell.bgImg.image = UIImage(named: "img.png")
  44. cell.label1.text = "..."
  45. cell.label2.text = "..."
  46.  
  47. return cell
  48.  
  49. class CustomCollectionViewCell: UICollectionViewCell {
  50.  
  51. override init(frame: CGRect) {
  52. super.init(frame: frame)
  53.  
  54. let imageView = UIImageView(frame: self.bounds)
  55. //customise imageview
  56. imageView.backgroundColor = UIColor.red
  57. contentView.addSubview(imageView)
  58. let label = UILabel(frame: CGRect(x: 20, y: 20, width: self.bounds.width - 20, height: 20))
  59. //Customsize label
  60. label.text = "Hello"
  61. label.textColor = UIColor.white
  62. contentView.addSubview(label)
  63. }
  64.  
  65. required init?(coder aDecoder: NSCoder) {
  66. fatalError("init(coder:) has not been implemented")
  67. }
  68.  
  69. override var bounds: CGRect {
  70. didSet {
  71. contentView.frame = bounds
  72. }
  73. }
  74. }
Add Comment
Please, Sign In to add comment