ios 怎么解决colletionview头视图分组重复加载的问题
1个回答
展开全部
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)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询