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
    // 原因: 不支持从扩展中覆盖non-@objc声明
    // 解决:将方法写到主类
    
    override func setupUI() {
        super.setupUI()
        
        collectionView.addSubview(amuseMenuView)
        
        //经常使用 高度随着父控件的拉伸而拉伸。
//        collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        
        collectionView.contentInset = UIEdgeInsets(top:kTopHeaderViewH , left: 0, bottom: 0, right: 0)
    }
    
    override func loadData() {
           //给父类的 baseVM 赋值
              baseVM = amuseViewModel
           //请求数据
              amuseViewModel.requestAmuseData {
                  self.collectionView.reloadData()
                
                var tmpGroups = self.amuseViewModel.anchorGroups
                tmpGroups.removeFirst()
                self.amuseMenuView.groups = tmpGroups
                
                //数据请求完成
                self.loadDataFinished()
                
              }
       }
//
//    //对父类的 cell大小布局 重写
//     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()
        
        //经常使用 高度随着父控件的拉伸而拉伸。
//           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 }
        //计算多少页的算法
        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
        
        //判断越界问题 Change 'let' to 'var' to make it mutable 这个地方 endIndex是可变的  用var
        // 运算符号 要加空格
        if endIndex > groups!.count - 1 {
            endIndex = groups!.count - 1
        }
        
        //取出数据 再组装成一个数组
        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

类的属性

image.png
image.png

计算属性的完整写法

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

类属性的监听


image.png
image.png

属性发生改变

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()
            
            //数据请求完成
            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)
    }

}

重写父类的方法
UI布局全是在父类实现的,所有只要实现这个类的数据请求即可。

本期分享的内容很简单比较适合初学者看看。
源码地址已经在
JFDouYu-Swift(二中已经贴出来了) 再会。

你可能感兴趣的:(JFDouYu-Swift(三))