swift 基础学习(6) - UITableView UICollectionView

前端之家收集整理的这篇文章主要介绍了swift 基础学习(6) - UITableView UICollectionView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. // 遵守协议
  2. class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
  3.  
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. setUpUI()
  7. }
  8.  
  9.  
  10. // 创建UITableView
  11.  
  12. func setUpUI() {
  13. // 1 创建表格
  14. let tableView = UITableView(frame: view.bounds,style: .plain)
  15. //2 设置代理
  16. tableView.delegate = self
  17. tableView.dataSource = self
  18. // 3 添加到视图上
  19. view .addSubview(tableView)
  20. // 4 注册cell
  21. tableView.register(UITableViewCell.self,forCellReuseIdentifier: "cell_id")
  22. }
  23.  
  24. // 实现代理方法
  25.  
  26. // MARK: - 实现tableVIew的代理方法
  27. func numberOfSections(in tableView: UITableView) -> Int {
  28. return 1
  29. }
  30. func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
  31. return 20
  32. }
  33.  
  34. func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
  35. return 50
  36. }
  37. func tableView(_ tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
  38. return 0.0001
  39. }
  40. func tableView(_ tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
  41. return 0.0001
  42. }
  43. func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  44. let cell = tableView.dequeueReusableCell(withIdentifier: "cell_id",for: indexPath)
  45. cell.textLabel?.text = "这是第\(indexPath.row)行"
  46. return cell
  47. }
  1. // 遵守协议
  2.  
  3. class CollectionViewController: UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource{
  4.  
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. view.backgroundColor = UIColor.white
  8. setUpUI()
  9. }
  10.  
  11. // 创建 UICollectionView
  12.  
  13. func setUpUI() {
  14. let layout = UICollectionViewFlowLayout()
  15. let collectionView = UICollectionView(frame: view.bounds,collectionViewLayout: layout)
  16. collectionView.backgroundColor = UIColor.white
  17. collectionView.delegate = self
  18. collectionView.dataSource = self;
  19. view.addSubview(collectionView)
  20. collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "cell_id")
  21. }
  22.  
  23. // 实现代理方法
  24.  
  25. func numberOfSections(in collectionView: UICollectionView) -> Int {
  26. return 1
  27. }
  28. func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
  29. return 20
  30. }
  31. func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  32. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_id",for: indexPath)
  33. cell.backgroundColor = UIColor.green
  34. return cell
  35. }
  36.  
  37. func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
  38. return CGSize(width: 100,height: 100)
  39. }

猜你在找的Swift相关文章