JFDouYu-Swift(三)

image.png
class AmuseViewController: BaseAnchorViewController {
    
    private lazy var amuseMenuView:JFAmuseMenuView = {
        let  view = JFAmuseMenuView.amuseMenuView()
        view.frame = CGRect(x: 0, y: -kTopHeaderViewH, width: kScreenWidth, height: kTopHeaderViewH)
        return view
        }()
    
    private lazy var amuseViewModel:JFAmuseViewModel = JFAmuseViewModel()
    
    
    //Overriding non-@objc declarations from extensions is not supported
    // 原因: 不支持從擴(kuò)展中覆蓋non-@objc聲明
    // 解決:將方法寫(xiě)到主類(lèi)
    
    override func setupUI() {
        super.setupUI()
        
        collectionView.addSubview(amuseMenuView)
        
        //經(jīng)常使用 高度隨著父控件的拉伸而拉伸。
//        collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        
        collectionView.contentInset = UIEdgeInsets(top:kTopHeaderViewH , left: 0, bottom: 0, right: 0)
    }
    
    override func loadData() {
           //給父類(lèi)的 baseVM 賦值
              baseVM = amuseViewModel
           //請(qǐng)求數(shù)據(jù)
              amuseViewModel.requestAmuseData {
                  self.collectionView.reloadData()
                
                var tmpGroups = self.amuseViewModel.anchorGroups
                tmpGroups.removeFirst()
                self.amuseMenuView.groups = tmpGroups
                
                //數(shù)據(jù)請(qǐng)求完成
                self.loadDataFinished()
                
              }
       }
//
//    //對(duì)父類(lèi)的 cell大小布局 重寫(xiě)
//     override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
//           return CGSize(width: kItemW, height: kNormalItemH)
//    }
    
}

image.png
private let JFMenuID:String = "JFMenuID"

class JFAmuseMenuView: UIView {
    
    //定義屬性
    var groups : [AnchorGroup]?{
        didSet{
            collectionView.reloadData()
        }
    }

    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var pageController: UIPageControl!
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //經(jīng)常使用 高度隨著父控件的拉伸而拉伸。
//           collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        //不隨著父控件的拉伸而拉伸
        autoresizingMask = UIView.AutoresizingMask()
        
        collectionView.dataSource = self
        collectionView.delegate = self
//        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: JFAmuseMenuViewCellID)
        
        collectionView.register(UINib(nibName: "JFAmuseMenuViewCell", bundle: nil), forCellWithReuseIdentifier: JFMenuID)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let collectionViewLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        collectionViewLayout.itemSize = collectionView.bounds.size
    }
    
}

extension JFAmuseMenuView{
    class func amuseMenuView() -> JFAmuseMenuView {
        return Bundle.main.loadNibNamed("JFAmuseMenuView", owner: nil, options: nil)?.first as! JFAmuseMenuView
    }
}

extension JFAmuseMenuView:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if groups == nil  { return 0 }
        //計(jì)算多少頁(yè)的算法
        let pageNum = (groups!.count - 1) / 8 + 1
        pageController.numberOfPages = pageNum
        return pageNum
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: JFMenuID, for: indexPath) as! JFAmuseMenuViewCell
        setCellDataWithCell(cell: cell, indexPath: indexPath)
        return cell
    }
    
    private func setCellDataWithCell(cell:JFAmuseMenuViewCell,indexPath:IndexPath){
        // 0-7
        // 8-15
        // 15-23
        let startIndex = indexPath.item * 8
        var endIndex = (indexPath.item + 1) * 8 - 1
        
        //判斷越界問(wèn)題 Change 'let' to 'var' to make it mutable 這個(gè)地方 endIndex是可變的  用var
        // 運(yùn)算符號(hào) 要加空格
        if endIndex > groups!.count - 1 {
            endIndex = groups!.count - 1
        }
        
        //取出數(shù)據(jù) 再組裝成一個(gè)數(shù)組
        cell.groups = Array(groups![startIndex...endIndex])

    }
}

extension JFAmuseMenuView : UICollectionViewDelegate{
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        pageController.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.width)
    }
    
}

UIView上添加UICollectionView

class JFAmuseMenuViewCell: UICollectionViewCell {
    
    var groups:[AnchorGroup]?{
        didSet{
            collectionView.reloadData()
        }
    }
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "JFCollectionGameCell", bundle: nil), forCellWithReuseIdentifier:JFGameCellID )
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
         let collectionViewLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        let itemW  = contentView.bounds.width / 4
        let itemH  = contentView.bounds.height / 2
         collectionViewLayout.itemSize = CGSize(width: itemW, height: itemH)
        
    }

}

extension JFAmuseMenuViewCell : UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return groups?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: JFGameCellID, for: indexPath) as! JFCollectionGameCell
        //超出部分不展示
        cell.clipsToBounds = true
        cell.baseGame = groups![indexPath.item]
        return cell
    }
}

UICollevtionView 的cell里面又嵌套 UICollevtionView

類(lèi)的屬性

image.png
image.png

計(jì)算屬性的完整寫(xiě)法

image.png
image.png
image.png
image.png

類(lèi)屬性的監(jiān)聽(tīng)


image.png
image.png

屬性發(fā)生改變

image.png
image.png
image.png
image.png
private let kItemMargin:CGFloat = 10
private let kItemW:CGFloat = (kScreenWidth - kItemMargin * 3)/2
private let kNormalItemH:CGFloat = kItemW * 3 / 4

class FunnyViewController: BaseAnchorViewController {
    
    private lazy var funnyVM:JFFunnyViewModel = JFFunnyViewModel()
    
    override func loadData() {
        baseVM = funnyVM

        funnyVM.loadFunnyData {
            self.collectionView.reloadData()
            
            //數(shù)據(jù)請(qǐng)求完成
            self.loadDataFinished()
            
            
        }
    }
    override func setupUI() {
        super.setupUI()
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.headerReferenceSize = CGSize.zero
        collectionView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
    }

}

重寫(xiě)父類(lèi)的方法
UI布局全是在父類(lèi)實(shí)現(xiàn)的,所有只要實(shí)現(xiàn)這個(gè)類(lèi)的數(shù)據(jù)請(qǐng)求即可。

本期分享的內(nèi)容很簡(jiǎn)單比較適合初學(xué)者看看。
源碼地址已經(jīng)在
JFDouYu-Swift(二中已經(jīng)貼出來(lái)了) 再會(huì)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • swift進(jìn)階 學(xué)習(xí)大綱[http://www.itdecent.cn/p/0fc67b373540] 上一節(jié)[...
    markhetao閱讀 1,849評(píng)論 0 9
  • 一、簡(jiǎn)介 空閑之余手撕一把springMVC,以加深對(duì)spring的理解,盡可能寫(xiě)的全面,源碼中注釋也會(huì)很詳細(xì)。話(huà)...
    azdream閱讀 558評(píng)論 0 0
  • 之前已經(jīng)看過(guò)好幾次Swift的語(yǔ)法規(guī)定,但是至此也沒(méi)用過(guò)幾次,所以難免就出現(xiàn)了遺漏或者忘記和混淆的情況,所以,這次...
    大鵬鳥(niǎo)閱讀 434評(píng)論 0 0
  • 久違的晴天,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí),離放學(xué)已經(jīng)沒(méi)多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,866評(píng)論 16 22
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開(kāi)了第一次的黨會(huì),身份的轉(zhuǎn)變要...
    余生動(dòng)聽(tīng)閱讀 10,912評(píng)論 0 11

友情鏈接更多精彩內(nèi)容