小碼哥-鬥魚直播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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章