前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色

Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色

作者头像
热心的程序员
发布2018-08-30 10:32:10
4.9K0
发布2018-08-30 10:32:10
举报
文章被收录于专栏:编程之路编程之路

上一篇介绍了如何使用swift纯代码构建UIColletionView,本篇继续介绍如何对其分组、设置分组标题、cell 圆角、选中变色。

效果图如下:

效果图

1.设置Header布局SHomeHeader,继承自UICollectionReusableView。
代码语言:javascript
复制
//
//  SHomeHeader.swift
//
//  Created by wangjie on 16/5/4.
//  Copyright © 2016年 wangjie. All rights reserved.
//

import UIKit

class SHomeHeader: UICollectionReusableView {
    var titleLabel:UILabel?//title

    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    func initView(){
        titleLabel = UILabel(frame: CGRectMake(0, 0, SCREEN_WIDTH, 30))
        titleLabel?.backgroundColor = HEADER_BG_COLOR
        self .addSubview(titleLabel!)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
2.为UICollectionReusableView注册header。
代码语言:javascript
复制
//注册header
collectionView!.registerClass(SHomeHeader.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
3.自定义Header并设置宽高。
代码语言:javascript
复制
//设置HeadView的宽高
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
        return CGSize(width: SCREEN_WIDTH, height: headerHeight)
    }
    
    //返回自定义HeadView
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
        var v = SHomeHeader()
        if kind == UICollectionElementKindSectionHeader{
            v = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerIdentifier, forIndexPath: indexPath) as! SHomeHeader
            let title:String = headerArr[indexPath.section] as! String
            v.titleLabel?.text = title
        }
        return v
    }
4.设置选中颜色及圆角Cell。
代码语言:javascript
复制
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell!.layer.cornerRadius = 4
        cell?.backgroundColor = UIColor.yellowColor()
    }
    
    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell!.layer.cornerRadius = 4
        cell?.backgroundColor = UIColor.whiteColor()
    }
5.自定义圆角带边框的UICollectionViewCell。
代码语言:javascript
复制
//
//  SHomeCell.swift
//
//  Created by wangjie on 16/5/4.
//  Copyright © 2016年 wangjie. All rights reserved.
//

import UIKit

class SHomeCell: UICollectionViewCell {
    
    var titleLabel:UILabel?//title
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initView(){
        titleLabel = UILabel(frame: CGRectMake(0, 0, (SCREEN_WIDTH - 20)/3, (SCREEN_WIDTH - 20)/3))
        titleLabel?.layer.cornerRadius = 4
        titleLabel?.layer.borderWidth = 0.5
        titleLabel?.textAlignment = NSTextAlignment.Center
        titleLabel?.layoutMargins = UIEdgeInsets(top:0, left:0, bottom:0, right:0)

        self .addSubview(titleLabel!)
    }
}
6. UIViewController 完整代码。
代码语言:javascript
复制
//
//  SHomeViewController.swift
//
//  Created by wangjie on 16/5/4.
//  Copyright © 2016年 wangjie. All rights reserved.
//

import UIKit

class SHomeViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    var collectionView : UICollectionView?
    var dataArr = NSMutableArray()//数据源
    var headerArr = NSMutableArray()//分组标题
    let headerHeight:CGFloat = 30
    let cellHeight:CGFloat = (SCREEN_WIDTH - 20)/3
    let headerIdentifier:String = "headView"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
        initData()
    }
    
    func initView(){
        let layout = UICollectionViewFlowLayout()
        self.view.backgroundColor = UIColor.whiteColor()
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        layout.minimumInteritemSpacing = 5; // this number could be anything <=5. Need it here because the default is 10.
        layout.minimumLineSpacing = 4.0 //设置行间距
        layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20)/3, (SCREEN_WIDTH - 20)/3) // 20 is 2*5 for the 2 edges plus 2*5 for the spaces between the cells
        
        collectionView = UICollectionView(frame: CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT), collectionViewLayout: layout)
        //注册一个cell
        collectionView!.registerClass(SHomeCell.self, forCellWithReuseIdentifier:"cell")
        collectionView?.delegate = self;
        collectionView?.dataSource = self;
        collectionView?.backgroundColor = UIColor.whiteColor()
        //设置每一个cell的宽高
        self.view.addSubview(collectionView!)
        //注册header
        collectionView!.registerClass(SHomeHeader.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
        
    }
    
    func initData(){
        initHeaderData()
        initSelectionData()
        self.collectionView?.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //返回多少个组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return headerArr.count
    }
    
    //返回多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    //返回自定义的cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SHomeCell
        let title = dataArr[indexPath.row]
        cell.titleLabel?.text = title as? String
        return cell
        
    }
    
    func initHeaderData() {
        
        headerArr.addObject("header 1")
        headerArr.addObject("header 2")
        headerArr.addObject("header 3")
        
    }
    
    func initSelectionData() {
        
        dataArr.addObject("selection 1")
        dataArr.addObject("selection 2")
        dataArr.addObject("selection 3")
        
    }
    
    
    //设置HeadView的宽高
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
        return CGSize(width: SCREEN_WIDTH, height: headerHeight)
    }
    
    //返回自定义HeadView
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
        var v = SHomeHeader()
        if kind == UICollectionElementKindSectionHeader{
            v = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerIdentifier, forIndexPath: indexPath) as! SHomeHeader
            let title:String = headerArr[indexPath.section] as! String
            v.titleLabel?.text = title
        }
        return v
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell!.layer.cornerRadius = 4
        cell?.backgroundColor = UIColor.yellowColor()
    }
    
    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell!.layer.cornerRadius = 4
        cell?.backgroundColor = UIColor.whiteColor()
    }
    
}
7. 完整代码见:SwiftCollectionViewDemo
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016.05.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.设置Header布局SHomeHeader,继承自UICollectionReusableView。
  • 2.为UICollectionReusableView注册header。
  • 3.自定义Header并设置宽高。
  • 4.设置选中颜色及圆角Cell。
  • 5.自定义圆角带边框的UICollectionViewCell。
  • 6. UIViewController 完整代码。
  • 7. 完整代码见:SwiftCollectionViewDemo。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档