小码哥-斗鱼直播APP之娱乐基本展示

娱乐基本展示

效果展示

  • 如图
    !

内容的展示

界面布局

  • 内容的展示依然是一个UICollectionView

    • 懒加载UICollectionView
    • 将UICollectionView添加到控制器的View中
    • 实现数据源&代理
  • 懒加载UICollectionView

  1. private let kItemMargin : CGFloat = 10
  2. private let kItemW = (kScreenW - 3 * kItemMargin) / 2
  3. private let kNormalItemH = kItemW * 3 / 4
  4. private let kPrettyItemH = kItemW * 4 / 3
  5. private let kHeaderViewH : CGFloat = 50
  6. private let kNormalCellID = "kNormalCellID"
  7. private let kPrettyCellID = "kPrettyCellID"
  8. private let kHeaderViewID = "kHeaderViewID"
  9. class AmuseViewController: UIViewController {
  10. // MARK: 懒加载属性
  11. fileprivate lazy var collectionView : UICollectionView = {[unowned self] in
  12. // 1.创建布局
  13. let layout = UICollectionViewFlowLayout()
  14. layout.itemSize = CGSize(width: kItemW, height: kNormalItemH)
  15. layout.minimumLineSpacing = 0
  16. layout.minimumInteritemSpacing = kItemMargin
  17. layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
  18. layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
  19. // 2.创建UICollectionView
  20. let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
  21. collectionView.backgroundColor = UIColor.white
  22. collectionView.dataSource = self
  23. collectionView.delegate = self
  24. collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  25. collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNormalCellID)
  26. collectionView.register(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCellID)
  27. collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
  28. return collectionView
  29. }()
  30. // MARK: 系统回调
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. setupUI()
  34. }
  35. }
  • 实现数据源&代理方法
  1. // MARK:- 遵守UICollectionView的数据源&代理协议
  2. extension AmuseViewController : UICollectionViewDataSource, UICollectionViewDelegate {
  3. func numberOfSections(in collectionView: UICollectionView) -> Int {
  4. return 8
  5. }
  6. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  7. return 4
  8. }
  9. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  10. // 1.获取Cell
  11. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNormalCellID, for: indexPath)
  12. cell.backgroundColor = UIColor.randomColor()
  13. return cell
  14. }
  15. }

请求数据&展示数据

  1. class AmuseViewModel {
  2. fileprivate lazy var anchorGroups : [AnchorGroup] = [AnchorGroup]()
  3. }
  4. extension AmuseViewModel {
  5. func loadAmuseData(finishedCallback : @escaping () -> ()) {
  6. NetworkTools.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/getHotRoom/2") { (result) in
  7. // 1.获取数据
  8. guard let resultDict = result as? [String : Any] else { return }
  9. guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
  10. // 2.字典转模型
  11. for dict in dataArray {
  12. self.anchorGroups.append(AnchorGroup(dict: dict))
  13. }
  14. // 3.回调数据
  15. finishedCallback()
  16. }
  17. }
  18. }
  • 控制器中展示数据
    • 修改之前的数据源&代理
  1. fileprivate func loadData() {
  2. amuseVM.loadAmuseData {
  3. self.collectionView.reloadData()
  4. }
  5. }

父类抽取

  • 展示内容,我们会发现,该界面和推荐界面相似度非常非常高
    • 相似:添加UICollectionView,并且每组有对应的HeaderView
    • 不同:推荐界面第1组使用的是PrettyCell
  • 思考:
    • 既然相似度很高,那么我们可以抽取父类
    • 将相同代码抽取到父类中,不同代码子类自己来实现
  • 请求数据的ViewModel的抽取
  1. class BaseViewModel {
  2. lazy var anchorGroups : [AnchorGroup] = [AnchorGroup]()
  3. }
  4. extension BaseViewModel {
  5. func loadAnchorData(URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping () -> ()) {
  6. NetworkTools.requestData(.get, URLString: URLString, parameters: parameters) { (result) in
  7. // 1.获取数据
  8. guard let resultDict = result as? [String : Any] else { return }
  9. guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
  10. // 2.字典转模型
  11. for dict in dataArray {
  12. self.anchorGroups.append(AnchorGroup(dict: dict))
  13. }
  14. // 3.回调数据
  15. finishedCallback()
  16. }
  17. }
  18. }
  • 抽取懒加载UICollectionView
    • 两个控制器都需要懒加载一个UICollectionView
    • 并且UICollectionView需要设置的内容和尺寸也是一致的
  • 实现数据源&代理
    • 无论是推荐还是娱乐都需要成为UICollectionView的数据源&代理
    • 如果子类有不同的实现,可以让子类自己实现
  1. private let kItemMargin : CGFloat = 10
  2. private let kHeaderViewH : CGFloat = 50
  3. let kItemW = (kScreenW - 3 * kItemMargin) / 2
  4. let kNormalItemH = kItemW * 3 / 4
  5. let kPrettyItemH = kItemW * 4 / 3
  6. private let kNormalCellID = "kNormalCellID"
  7. private let kHeaderViewID = "kHeaderViewID"
  8. let kPrettyCellID = "kPrettyCellID"
  9. class BaseAnchorViewController: UIViewController {
  10. // MARK: 懒加载属性
  11. var baseVM : BaseViewModel!
  12. lazy var collectionView : UICollectionView = {[unowned self] in
  13. // 1.创建布局
  14. let layout = UICollectionViewFlowLayout()
  15. layout.itemSize = CGSize(width: kItemW, height: kNormalItemH)
  16. layout.minimumLineSpacing = 0
  17. layout.minimumInteritemSpacing = kItemMargin
  18. layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
  19. layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
  20. // 2.创建UICollectionView
  21. let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
  22. collectionView.backgroundColor = UIColor.white
  23. collectionView.dataSource = self
  24. collectionView.delegate = self
  25. collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  26. collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNormalCellID)
  27. collectionView.register(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCellID)
  28. collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
  29. return collectionView
  30. }()
  31. // MARK: 系统回调
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. setupUI()
  35. loadData()
  36. }
  37. }
  38. // MARK:- 设置UI界面内容
  39. extension BaseAnchorViewController {
  40. func setupUI() {
  41. view.addSubview(collectionView)
  42. }
  43. func loadData() {
  44. }
  45. }
  46. // MARK:- 遵守UICollectionView的数据源&代理协议
  47. extension BaseAnchorViewController : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  48. func numberOfSections(in collectionView: UICollectionView) -> Int {
  49. return baseVM.anchorGroups.count
  50. }
  51. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  52. return baseVM.anchorGroups[section].anchors.count
  53. }
  54. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  55. // 1.获取Cell
  56. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNormalCellID, for: indexPath) as! CollectionNormalCell
  57. cell.anchor = baseVM.anchorGroups[indexPath.section].anchors[indexPath.item]
  58. return cell
  59. }
  60. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  61. // 1.取出headerView
  62. let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath) as! CollectionHeaderView
  63. headerView.group = baseVM.anchorGroups[indexPath.section]
  64. return headerView
  65. }
  66. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  67. return CGSize(width: kItemW, height: kNormalItemH)
  68. }
  69. }
  • 让RecommendViewController&AmuseViewController集成子BaseAnchorViewController

    • 修改对应的代码即可

来源:http://bbs.520it.com/forum.php?mod=viewthread&tid=2498
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章