• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift控件UICollectionView使用学习总结-Swift

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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
        }
    }
}

 效果如下图:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode415.字符串相加|AddStrings发布时间:2022-07-13
下一篇:
Swift之枚举类型&amp;结构体发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap