玩转【斗鱼直播APP】系列之娱乐基本展示作者:小码哥教育娱乐基本展示效果展示如图内容的展示界面布局内容的展示依然是一个UICollectionView懒加载UICollectionView将UICollectionView添加到控制器的View中实现数据源&代理懒加载UICollectionView1.privateletkItemMargin:CGFloat=102.privateletkItemW=(kScreenW-3*kItemMargin)/23.privateletkNormalItemH=kItemW*3/44.privateletkPrettyItemH=kItemW*4/35.privateletkHeaderViewH:CGFloat=506.7.privateletkNormalCellID="kNormalCellID"8.privateletkPrettyCellID="kPrettyCellID"9.privateletkHeaderViewID="kHeaderViewID"10.11.classAmuseViewController:UIViewController{12.13.//MARK:懒加载属性14.fileprivatelazyvarcollectionView:UICollectionView={[unownedself]in15.//1.创建布局16.letlayout=UICollectionViewFlowLayout()17.layout.itemSize=CGSize(width:kItemW,height:kNormalItemH)18.layout.minimumLineSpacing=019.layout.minimumInteritemSpacing=kItemMargin20.layout.headerReferenceSize=CGSize(width:kScreenW,height:kHeaderViewH)21.layout.sectionInset=UIEdgeInsets(top:0,left:kItemMargin,bottom:0,right:kItemMargin)22.23.//2.创建UICollectionView24.letcollectionView=UICollectionView(frame:self.view.bounds,collectionViewLayout:layout)25.collectionView.backgroundColor=UIColor.white26.collectionView.dataSource=self27.collectionView.delegate=self28.collectionView.autoresizingMask=[.flexibleHeight,.flexibleWidth]29.30.collectionView.register(UINib(nibName:"CollectionNormalCell",bundle:nil),forCellWithReuseIdentifier:kNormalCellID)31.collectionView.register(UINib(nibName:"CollectionPrettyCell",bundle:nil),forCellWithReuseIdentifier:kPrettyCellID)32.collectionView.register(UINib(nibName:"CollectionHeaderView",bundle:nil),forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReuseIdentifier:kHeaderViewID)33.34.returncollectionView35.}()36.37.//MARK:系统回调38.overridefuncviewDidLoad(){39.super.viewDidLoad()40.41.setupUI()42.}43.44.}实现数据源&代理方法1.//MARK:-遵守UICollectionView的数据源&代理协议2.extensionAmuseViewController:UICollectionViewDataSource,UICollectionViewDelegate{3.funcnumberOfSections(incollectionView:UICollectionView)->Int{4.return85.}6.7.funccollectionView(_collectionView:UICollectionView,numberOfItemsInSectionsection:Int)->Int{8.return49.}10.11.funccollectionView(_collectionView:UICollectionView,cellForItemAtindexPath:IndexPath)->UICollectionViewCell{12.//1.获取Cell13.letcell=collectionView.dequeueReusableCell(withReuseIdentifier:kNormalCellID,for:indexPath)14.15.cell.backgroundColor=UIColor.randomColor()16.17.returncell18.}19.}请求数据&展示数据接口描述请求地址:http://capi.douyucdn.cn/api/v1/getHotRoom/2请求参数:无参数ViewModel封装1.classAmuseViewModel{2.fileprivatelazyvaranchorGroups:[AnchorGroup]=[AnchorGroup]()3.}4.5.extensionAmuseViewModel{6.funcloadAmuseData(finishedCallback:@escaping()->()){7.NetworkTools.requestData(.get,URLString:"http://capi.douyucdn.cn/api/v1/getHotRoom/2"){(result)in8.//1.获取数据9.guardletresultDict=resultas?[String:Any]else{return}10.guardletdataArray=resultDict["data"]as?[[String:Any]]else{return}11.12.//2.字典转模型13.fordictindataArray{14.self.anchorGroups.append(AnchorGroup(dict:dict))15.}16.17.//3.回调数据18.finishedCallback()19.}20.}21.}控制器中展示数据修改之前的数据源&代理1.fileprivatefuncloadData(){2.amuseVM.loadAmuseData{3.self.collectionView.reloadData()4.}5.}父类抽取展示内容,我们会发现,该界面和推荐界面相似...