UICollectionView —— Swift
对 Swift开发UICollectionView的用法总结
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的方式相同如下图
自定义collectionViewCell的.swift文件的代码:
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
}
}
}
效果如下图: