swift中UICollectionView的使用(headerviewfooterviewcellmodel)

swift中UICollectionView的使用(headerviewfooterviewcellmodel),第1张

概述github学习地址:https://github.com/potato512/SYSwiftLearning 效果图 源码 // MARK: - 数据func setLocalData(){ self.mainArray = NSMutableArray() for number in 1...5 { let text

github学习地址:https://github.com/potato512/SYSwiftLearning

效果图


源码

// MARK: - 数据func setLocalData(){        self.mainArray = NSMutableArray()        for number in 1...5        {            let text = String(format: "第 %d 个",arguments: [number])                        let model = Model()            model.text = text                        self.mainArray.addobject(model);        }}
// MARK: - 视图    func setUI(){        // 滚动方向        let layout = UICollectionVIEwFlowLayout()        layout.scrollDirection = UICollectionVIEwScrollDirection.Vertical        // 实例化        self.mainCollectionVIEw = UICollectionVIEw(frame: self.vIEw.bounds,collectionVIEwLayout: layout)        // 添加到父视图        self.vIEw.addSubvIEw(self.mainCollectionVIEw);        // 属性设置        self.mainCollectionVIEw.autoresizingMask = .FlexibleHeight        self.mainCollectionVIEw.backgroundcolor = UIcolor.bluecolor()        self.mainCollectionVIEw.dataSource = self        self.mainCollectionVIEw.delegate = self        // 注册一个cell        self.mainCollectionVIEw!.registerClass(CollectionVIEwCell.self,forCellWithReuseIDentifIEr:cellIDentifIEr)        // 注册一个headVIEw        self.mainCollectionVIEw!.registerClass(CollectionReusableVIEwheader.self,forSupplementaryVIEwOfKind:UICollectionElementKindSectionheader,withReuseIDentifIEr: headerIDentifIEr)        // 注册一个footVIEw        self.mainCollectionVIEw!.registerClass(CollectionReusableVIEwFooter.self,forSupplementaryVIEwOfKind:UICollectionElementKindSectionFooter,withReuseIDentifIEr: footerIDentifIEr)                // 设置页眉、页脚、cell的宽高,及间隔        // 方法1 属性化设置(显示有点异常),或方法2-代理方法设置//        layout.itemSize = CGSizeMake(wIDthCell,heightCell) // cell的宽高//        layout.minimumlinespacing = 10.0  // cell的上下间隔//        layout.minimumInteritemSpacing = 10.0 // cell的左右间隔//        layout.headerReferenceSize = CGSizeMake(wIDth,heightheader) // 页眉宽高//        layout.footerReferenceSize = CGSizeMake(wIDth,heightFooter) // 页脚宽高}
// MARK: - UICollectionVIEwDataSource,UICollectionVIEwDelegate    // MARK: header/footer的视图// 返回多少个组func numberOfSectionsInCollectionVIEw(collectionVIEw: UICollectionVIEw) -> Int{        return 10}    // 返回headVIEw的宽高func collectionVIEw(collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,referenceSizeforheaderInSection section: Int) -> CGSize{        return CGSize(wIDth: wIDth,height: heightheader)}    // 返回footvIEw的宽高func collectionVIEw(collectionVIEw: UICollectionVIEw,referenceSizeforFooterInSection section: Int) -> CGSize{        return CGSizeMake(wIDth,heightFooter)}    // 返回自定义headVIEw或者FootVIEw,我这里以headvIEw为例func collectionVIEw(collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,atIndexPath indexPath: NSIndexPath) -> UICollectionReusableVIEw{        var reusablevIEw:UICollectionReusableVIEw!                if kind == UICollectionElementKindSectionheader        {            reusablevIEw = collectionVIEw.dequeueReusableSupplementaryVIEwOfKind(kind,withReuseIDentifIEr: headerIDentifIEr,forIndexPath: indexPath) as! CollectionReusableVIEwheader            reusablevIEw.backgroundcolor = UIcolor.greencolor()                        (reusablevIEw as! CollectionReusableVIEwheader).label.text = String(format: "第 %d 个页眉",arguments: [indexPath.section])        }        else if kind == UICollectionElementKindSectionFooter        {            reusablevIEw = collectionVIEw.dequeueReusableSupplementaryVIEwOfKind(kind,withReuseIDentifIEr: footerIDentifIEr,forIndexPath: indexPath) as! CollectionReusableVIEwFooter            reusablevIEw.backgroundcolor = UIcolor.browncolor()                        (reusablevIEw as! CollectionReusableVIEwFooter).label.text = String(format: "第 %d 个页脚",arguments: [indexPath.section])        }                return reusablevIEw}
// MARK: - cell视图    // 返回多少个cellfunc collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int{        return self.mainArray.count}    /**注意cell的大小与间距的关系,即cell的相对于父视图的大小受上下左右间距的大小影响,以及每个row显示的cell个数的影响。主要是计算宽度。如:每行显示2个,且上下左右间距为10.0,那么cell的大小相对于父视图来计算则是:cell的宽 = (父视图的宽 - 左右间距的大小 * (2 + 1)) / 2;cell的*/// 返回cell的宽高func collectionVIEw(collectionVIEw: UICollectionVIEw!,layout collectionVIEwLayout: UICollectionVIEwLayout!,sizeforItemAtIndexPath indexPath: NSIndexPath!) -> CGSize{        return CGSizeMake(wIDthCell,heightCell)}    // 返回cell 上下左右的间距func collectionVIEw(collectionVIEw: UICollectionVIEw,insetForSectionAtIndex section: Int) -> UIEdgeInsets{        return UIEdgeInsetsMake(10.0,10.0,10.0)}    // 返回自定义的cellfunc collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell{        let cell = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr(cellIDentifIEr,forIndexPath: indexPath) as! CollectionVIEwCell        cell.layer.borderWIDth = 0.3;        cell.layer.bordercolor = UIcolor.lightGraycolor().CGcolor                let model = self.mainArray.objectAtIndex(indexPath.row) as! Model        let text = model.text        cell.label!.text = text        return cell}
// MARK: 点击事件    func collectionVIEw(collectionVIEw: UICollectionVIEw,dIDSelectItemAtIndexPath indexPath: NSIndexPath) {                let text = String(format: ("你点击了第 %d-%d 个cell"),arguments: [indexPath.section + 1,indexPath.row + 1])        let alert = UIAlertVIEw(Title: nil,message: text,delegate: nil,cancelbuttonTitle: "知道了")        alert.show()}



// 自定义cellimport UIKitlet cellIDentifIEr = "CollectionVIEwCell"class CollectionVIEwCell: UICollectionVIEwCell {        var label:UILabel!        overrIDe init(frame: CGRect) {                super.init(frame: frame)                // 初始化各种控件        self.label = UILabel(frame: CGRectMake(5.0,5.0,(wIDthCell - 5.0 * 2),(heightCell - 5.0 * 2)))        self.label.numberOflines = 0        self.label.Font = UIFont.boldSystemFontOfSize(14.0)        self.label.textcolor = UIcolor.lightGraycolor()        self.contentVIEw.addSubvIEw(self.label)        self.contentVIEw.backgroundcolor = UIcolor.redcolor()        self.label.backgroundcolor = UIcolor.yellowcolor()    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}

// 自定义headervIEwimport UIKitlet headerIDentifIEr = "CollectionReusableVIEwheader"class CollectionReusableVIEwheader: UICollectionReusableVIEw {        var label:UILabel!        overrIDe init(frame: CGRect) {        super.init(frame: frame)                self.label = UILabel(frame: CGRectMake(10.0,0.0,(CGRectGetWIDth(self.bounds) - 10.0 * 2),heightheader))        self.addSubvIEw(self.label)    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}

// 自定义footer vIEwimport UIKitlet footerIDentifIEr = "CollectionReusableVIEwFooter"class CollectionReusableVIEwFooter: UICollectionReusableVIEw {        var label:UILabel!        overrIDe init(frame: CGRect) {        super.init(frame: frame)                self.label = UILabel(frame: CGRectMake(10.0,heightFooter))        self.addSubvIEw(self.label)    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    }

// 自定义modelimport UIKitlet wIDth = UIScreen.mainScreen().bounds.size.wIDthlet wIDthCell = (wIDth - 10.0 * 3) / 2let heightheader:CGfloat = 40.0let heightFooter:CGfloat = 20.0let heightCell:CGfloat = 80.0class Model: NSObject {    var text:String!        overrIDe init() {        super.init()    }    }
总结

以上是内存溢出为你收集整理的swift中UICollectionView的使用(headerview/footerview/cell/model)全部内容,希望文章能够帮你解决swift中UICollectionView的使用(headerview/footerview/cell/model)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1070115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存