UICollectionView —— Swiftswift
對 Swift開發UICollectionView的用法總結ide
import UIKit class ThirdViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { let item_identifier: String = "ThirdVCCollCell" let headerView_identifier: String = "ThirdVCCollHeaderView" let vcClassArr = ["UIScrollViewStudyVc","UITextFieldStudyVc","UILabelStudyVc"] var infoCollectionView:UICollectionView? = nil override func viewDidLoad() { super.viewDidLoad() print("vcClassArr:\(vcClassArr)") view.addSubview(self.initInfoCollectionView()) // Do any additional setup after loading the view. } func initInfoCollectionView() -> UICollectionView { let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width:80,height: 60) //設置item尺寸 layout.minimumLineSpacing = (UIScreen.main.bounds.width - 240) * 0.2 //上下間隔 layout.minimumInteritemSpacing = (UIScreen.main.bounds.width - 240) * 0.1 //左右間隔 layout.headerReferenceSize = CGSize(width:10,height: 30) //頭部間隔 layout.footerReferenceSize = CGSize(width:0,height: 0) //底部間隔 layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10) //section四周的縮進 layout.scrollDirection = UICollectionViewScrollDirection.vertical //滾動方向 self.infoCollectionView = UICollectionView.init(frame: view.bounds, collectionViewLayout: layout) self.infoCollectionView?.delegate = self self.infoCollectionView?.dataSource = self self.infoCollectionView?.backgroundColor = UIColor.white //註冊cell(使用xib自定義的collectionViewCell) self.infoCollectionView?.register(UINib.init(nibName: "ThirdVCCollCell", bundle: nil), forCellWithReuseIdentifier: item_identifier) //註冊headerView self.infoCollectionView?.register(ThirdVCCollHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerView_identifier) return self.infoCollectionView! } func numberOfSections(in collectionView: UICollectionView) -> Int { return 6 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 11 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let itemCell: ThirdVCCollCell = collectionView.dequeueReusableCell(withReuseIdentifier: item_identifier, for: indexPath) as! ThirdVCCollCell itemCell.backgroundColor = UIColor.red; itemCell.setTitleLabelText(titleText:"第\(indexPath.section + 1)組") itemCell.setSubTitleLabelText(titleText:"第\(indexPath.item + 1)個Item") return itemCell; } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("當前點擊第\(indexPath.section + 1)組,第\(indexPath.item + 1)個Item") if indexPath.section == 0{ if indexPath.row < 3{ let vcClassName = self.vcClassArr[indexPath.section] //字符串轉 class let vcClass = NSClassFromString("swift_study1." + vcClassName) as! NSObject.Type;//"包名.類名" let vc:UIViewController = vcClass.init() as! UIViewController; vc.hidesBottomBarWhenPushed = true self.navigationController?.pushViewController(vc, animated: true) } } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize.init(width: self.view.frame.size.width, height: 30.0) } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { if kind == UICollectionElementKindSectionHeader{ let headerView:ThirdVCCollHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerView_identifier, for: indexPath) as! ThirdVCCollHeaderView headerView.setTitleLabelText(title: "第\(indexPath.section)組") return headerView }else if kind == UICollectionElementKindSectionFooter{ } return UICollectionReusableView() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
xib自定義collectionViewCell的方法:佈局
其中自定義的collectionViewCell的.xib文件的佈局約束和object-c的方式相同以下圖spa
自定義collectionViewCell的.swift文件的代碼:3d
import UIKit class ThirdVCCollCell: UICollectionViewCell { @IBOutlet weak var title_label: UILabel! @IBOutlet weak var subTitle_label: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } func setTitleLabelText(titleText:String){ if !titleText.isEmpty{ self.title_label.text = titleText } } func setSubTitleLabelText(titleText:String){ if !titleText.isEmpty{ self.subTitle_label.text = titleText } } }
效果以下圖:code