ios 怎么解决colletionview头视图分组重复加载的问题

 我来答
ID千里一醉
2016-03-20 · TA获得超过3228个赞
知道小有建树答主
回答量:963
采纳率:0%
帮助的人:651万
展开全部
  import UIKit class Model: NSObject { var title:String? var desc:String? } HeaderReusableView.swift import UIKit //分组头部重用视图 class HeaderReusableView: UICollectionReusableView { var headerLb:UILabel! override init(frame: CGRect) { super.init(frame: frame) headerLb=UILabel() headerLb.frame=CGRectMake(5, 0, kWidth-10, 40) headerLb.backgroundColor=UIColor.yellowColor() self .addSubview(headerLb!) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } FooterReusableView.swift import UIKit //分组脚步重用视图 class FooterReusableView: UICollectionReusableView { var footerLb:UILabel! override init(frame: CGRect) { super.init(frame: frame) footerLb=UILabel() footerLb.frame=CGRectMake(5, 0, kWidth-10, 30) footerLb.backgroundColor=UIColor.cyanColor() self.addSubview(footerLb!) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } ViewController.swift import UIKit let kWidth=UIScreen.mainScreen().bounds.size.width let kHeight=UIScreen.mainScreen().bounds.size.height //如果要显示分组头视图或脚视图,必须遵守UICollectionViewDelegateFlowLayout,并实现返回头视图尺寸或脚视图尺寸的代理方法 class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { var dataArray:NSMutableArray = [] override func viewDidLoad() { super.viewDidLoad() createDataArray() createCollectionView() } //创建数据 func createDataArray(){ dataArray=NSMutableArray() for i in 65...90 { let items=NSMutableArray() for j in 1...10 { let m=Model() m.title=String(format:"第%c组第%d个数据",i,j) m.desc=String(format:"第%c组第%d个描述", i,j) items.addObject(m) } dataArray.addObject(items) } } //创建网格视图 func createCollectionView(){ //布局对象 let flowLayout=UICollectionViewFlowLayout() flowLayout.sectionInset=UIEdgeInsetsMake(5, 5, 5, 5) flowLayout.itemSize=CGSizeMake(180, 120) flowLayout.minimumInteritemSpacing=5; flowLayout.minimumLineSpacing=10; let frame=CGRectMake(0, 20, kWidth, kHeight-20) let collectionView=UICollectionView(frame: frame, collectionViewLayout: flowLayout) collectionView.delegate=self collectionView.dataSource=self collectionView.backgroundColor=UIColor.whiteColor() self.view.addSubview(collectionView) //注册cell collectionView.registerClass(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "cid") //注册header collectionView.registerClass(HeaderReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") //注册footer collectionView.registerClass(FooterReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer") } //item数 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { let array:NSMutableArray=dataArray.objectAtIndex(section) as! NSMutableArray return array.count } //组数 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return dataArray.count } //cell func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell:UICollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier("cid", forIndexPath: indexPath) cell.backgroundColor=UIColor.orangeColor() let m=dataArray.objectAtIndex(indexPath.section).objectAtIndex(indexPath.item) for sub in cell.contentView.subviews { sub.removeFromSuperview() } let titleLb=UILabel() titleLb.text=m.title titleLb.frame=CGRectMake(10, 10, 140, 40) titleLb.textAlignment=NSTextAlignment.Center cell.contentView .addSubview(titleLb) let descLb=UILabel() descLb.text=m.desc descLb.frame=CGRectMake(10, 60, 140, 40) descLb.textAlignment=NSTextAlignment.Center cell.contentView.addSubview(descLb) return cell } //分组头部、尾部 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind{ case UICollectionElementKindSectionHeader: let header:HeaderReusableView=collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! HeaderReusableView let index=indexPath.section+65 header.headerLb!.text=String(format: "第%c组头部", index) return header case UICollectionElementKindSectionFooter: let footer:FooterReusableView=collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath) as!FooterReusableView let index=indexPath.section+65 footer.footerLb!.text=String(format: "第%c组脚部", index) return footer default: return HeaderReusableView() } } //返回分组的头部视图的尺寸,在这里控制分组头部视图的高度 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSizeMake(UIScreen.mainScreen().bounds.size.width, 40) } //返回分组脚部视图的尺寸,在这里控制分组脚部视图的高度 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSizeMake(UIScreen.mainScreen().bounds.size.width, 40)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式