美文网首页
swift-UICollectionView

swift-UICollectionView

作者: Ismail丶 | 来源:发表于2018-06-13 10:27 被阅读0次

UICollectionView的基本使用

UICollectionView的使用跟tableview的使用大同小异!关键要注意在使用的cell的是有要先注册cell;使用自定义headerview也是需要先进行注册

初始化UICollectionViewFlowLayout

let jflayout = UICollectionViewFlowLayout.init()
jflayout.layoutAttributesForElements(in: CGRect.zero)
jflayout.scrollDirection = .vertical
jflayout.minimumLineSpacing = 1
jflayout.minimumInteritemSpacing = 1
jflayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
jflayout.itemSize = CGSize(width: (JF_SCREEN_WIDTH-2)/3, height: (JF_SCREEN_WIDTH-2)/3 + 15)

初始化UICollectionView等操作

jfRootView = UICollectionView.init(frame: view.frame, collectionViewLayout: jflayout)
view.addSubview(jfRootView)
jfRootView.delegate = self
jfRootView.dataSource = self
jfRootView.backgroundColor = UIColor.init(red: 244/255.0, green: 244/255.0, blue: 244/255.0, alpha: 1)

要想cell和collviewheader能正常使用必须先注册cell类型

//注册cell
jfRootView.register(JFMyCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "JFMyCollectionViewCell")
//注册Headerview
jfRootView.register(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UICollectionReusableView")

处理collectionView的代理方法,下面的两个方法就不多解释了大家都明白

// MARK:UICollectionView 代理方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return jfArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:JFMyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "JFMyCollectionViewCell", for: indexPath) as! JFMyCollectionViewCell
        let model:JFCellModel = jfArray[indexPath.row]
        cell.jfTitle.text = model.jfTitle
        cell.jfImgView.image = UIImage.init(named: model.jfImgName)
        return cell
}

给collectionView添加headerview

必须先设置collectionView header的大小

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: JF_SCREEN_WIDTH, height: 180)

}

进行自定义 headerView

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView{
        let reusableview = UICollectionReusableView()
        if kind == UICollectionElementKindSectionHeader {
            let jfNewView:UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UICollectionReusableView", for: indexPath)
            jfNewView.backgroundColor = JF_MAIN_COLOR
            jfNewView.addSubview(jfHeadImgView)
            jfHeadImgView.snp.makeConstraints { (make) in
                make.centerY.equalTo(jfNewView)
                make.size.equalTo(CGSize.init(width: 70, height: 70))
                make.left.equalTo(jfNewView.snp.left).offset(30)
            }

            jfHeadImgView.layer.borderWidth = 1
            jfHeadImgView.layer.borderColor = UIColor.white.cgColor
            jfHeadImgView.layer.masksToBounds = true
            jfHeadImgView.layer.cornerRadius = 70/2;
            jfHeadImgView.image = UIImage.init(named: "logo")
            let jfname = UILabel.init()
            jfNewView.addSubview(jfname)
            let jfhome = UILabel()
            jfNewView.addSubview(jfhome)
            jfname.textColor = UIColor.white
            jfhome.textColor = UIColor.white
            jfname.snp.makeConstraints { (make) in
                make.left.equalTo(jfHeadImgView.snp.right).offset(30)
                make.bottom.equalTo(jfHeadImgView.snp.centerY)
                make.size.equalTo(CGSize.init(width: 200, height: 30))
            }
            jfhome.snp.makeConstraints { (make) in
                make.left.equalTo(jfname)
                make.top.equalTo(jfHeadImgView.snp.centerY)
                make.size.equalTo(jfname)
            }

            jfname.text = JFUserModel.shareUser().Users_PersonName
            jfhome.text = JFUserModel.shareUser().Users_CorpName
            return jfNewView
     }     
  return reusableview
}

相关文章

网友评论

      本文标题:swift-UICollectionView

      本文链接:https://www.haomeiwen.com/subject/eyadeftx.html