IOS橫線滾動檢視的實現---方式二

weixin_34208283發表於2018-09-30
//初始化
 public var mDataList = [ReuniteMarketModel]()
    // MARK: - 懶載入九宮格分類按鈕
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.itemSize = CGSize(width:(YYScreenWidth-20)/3, height:100)
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView.init(frame:.zero, collectionViewLayout: layout)
        collectionView.contentSize = CGSize.init(width: YYScreenWidth-20, height: 100)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HomeSelfSelectCell.self, forCellWithReuseIdentifier:"SelfCellIdentifier")
        
        return collectionView
    }()

self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(title1.snp.bottom).offset(10)
            make.height.equalTo(100)
        }


//實現代理

extension HomeHeadViewNew: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.mDataList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:HomeSelfSelectCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SelfCellIdentifier", for: indexPath) as! HomeSelfSelectCell
        cell.backgroundColor = UIColor.white
        cell.mAllMarketModel = self.mDataList[indexPath.row]
        return cell
    }
}


//載入資料
self.collectionView.reloadData()


//cell

class HomeSelfSelectCell: UICollectionViewCell{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
   
    
    func setUpUI(){

      
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    var mAllMarketModel: ReuniteMarketModel? {
        didSet {

        }
    }

}



相關文章