swift中collectionView的用法

  • collectionView是iOS中一个非常重要的空间,但是使用的频率可能不高,方法相对来说和tableview有所不同,记录一下,防止遗忘
  • 1.创建UICollectionViewLayout
  • 2.创建UICollectionView
  • 3.设置数据源方法
  • 4.设置代理方法
  • 5.注册cell
1.创建UICollectionViewLayout
  • 过去一直认为collectionView比较难使用,尤其是UICollectionViewLayout,不知道该怎么设置,后来突然想到了,其实UICollectionViewLayout和tableview的UITableViewStyle.Plain是一样的,就是一个必设的属性。只不过**UICollectionViewLayout **是具体的item的大小,样式,间距等等属性,我们要用具体的flow实例化对象
     private let flowLayout : UICollectionViewFlowLayout = {
        let flt = UICollectionViewFlowLayout()
        flt.minimumLineSpacing = 0
        flt.minimumInteritemSpacing = 0
        flt.scrollDirection = UICollectionViewScrollDirection.Horizontal
        flt.itemSize = UIScreen.mainScreen().bounds.size
        return flt
    }()
2.创建UICollectionView
  • 因为我认为,collectionView的生成,他要变成什么样子的,他自己最清楚,所以就讲flowLayout的的创建全部放到collectionViewController的内部
    init(){   
        super.init(collectionViewLayout:flowLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

**init 之所以没有去写override,是因为collectionVC默认的方法是init(collectionViewLayout layout: UICollectionViewLayout) **

设置一下collectionView的基本属性,可以分页,和横向滑动,竖直方法滑动

        self.collectionView!.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
3.设置数据源方法

如果vc继承于UICollectionViewController的话,extension方法这样写

extension WXNewFeatureController{
    
    // MARK: UICollectionViewDataSource
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! WXNewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
}
4.设置代理方法

代理方法可以写,也可以不写~~

5.注册cell

如果懒得自定义就用系统的

  //声明一个常量

private let reuseIdentifier = "Cell"
//去注册
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(WXNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
//调用
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        return cell
    }
6.自定义cell

因为一直习惯封装,而且可以各种自定义内部控件,所以就去封装一下,这个cell就是在这个collectionViewController中使用,所以我么就将这个cell的创建放到这个文件中,而且创建可以使用private修饰,方法也可以用它修饰,在这个文件中,Private修饰的都可以随意调用,哪怕不是同一个类都行

private class WXNewFeatureCell:UICollectionViewCell{
    //对外开放一个index属性
    var imageIndex : NSInteger? {
        didSet{
           imageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            lab.text = "new_feature_\(imageIndex! + 1)"
            //判断如果是第4个页面,我们就去添加一个按钮
            if imageIndex! + 1 == 4 {
                confirmBtn.hidden = false
                contentView.addSubview(confirmBtn)
            }
        }
    }
    //懒加载数据
    private lazy var imageView : UIImageView = {
         return UIImageView()
    }()
    
    private lazy var lab : UILabel = UILabel()
  
    private lazy var confirmBtn:UIButton =  {
       let btn = UIButton()
        btn.hidden = true
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        return btn
    }()
      //MARK:-自定义一个collctionView
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //创建子控件
  private func setupUI(){
   contentView.addSubview(imageView)
    contentView.addSubview(lab)
//创建完子控件,必须调用这个方法,否则不去调用布局方法
    setNeedsUpdateConstraints()
    }
    //初始化的时候,如果不去调用的话,不走这个方法,很恶心,所以初始化完毕一定要去调用一下
    override func updateConstraints() {
        super.updateConstraints()
        imageView.autoPinEdgesToSuperviewEdges()
        lab.autoPinEdgesToSuperviewEdges()
        if imageIndex == 3 {
            let size = confirmBtn.currentBackgroundImage?.size
            confirmBtn.autoSetDimensionsToSize(size!)
            confirmBtn.autoAlignAxisToSuperviewAxis(ALAxis.Vertical)
            confirmBtn.autoPinEdgeToSuperviewEdge(ALEdge.Bottom, withInset: 100)
        }
    }
    
}
7.flowLayout可以的自定义

我太懒,就不写了,可以在这个文件中写,没关系~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容