iOS开发 - UICollectionView点击展开收起

作者: 小黑Swift | 来源:发表于2016-09-05 13:03 被阅读3979次

结合SB,快速实现UICollectionView点击展开收起

效果图 SB正常布局

主要:要设置好对应的 Identifier


ViewController.swift

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.delegate = self
            collectionView.dataSource = self
        }
    }

    var sectionState = [Bool]() // section 状态-记录 false 关 true 开
    var dataArray = [AnyObject]()
}

extension ViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initCollectionData()
    }
    
    //初始化数据
    func initCollectionData() {
        
        let first = ["1","2","3","4","5","6","1","2","3","4"]
        let second = ["1","2","3","4","5","6","7","8"]
        let third = ["1","2","3","4","5","6"]
        dataArray = [first,second,third]
        
        dataArray.forEach {_ in
            sectionState.append(false) //默认是关闭状态
        }
    }
    
    //分区的头部 - 点击事件
    func buttonClick(sender: UIButton) {
        sectionState[sender.tag] = sectionState[sender.tag] == true ? false : true
        collectionView.reloadSections(NSIndexSet(index: sender.tag))
    }
}

// MARK: - UICollectionView 代理方法
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return dataArray.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return sectionState[section] == true ? dataArray[section].count : 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GroupCollectionCell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.grayColor()
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "GroupSection", forIndexPath: indexPath) as! GroupSection
        header.sectionTitle.text = sectionState[indexPath.section] ? "关闭" : "打开"
        header.sectionBtn.addTarget(self, action: #selector(ViewController.buttonClick(_:)), forControlEvents: .TouchUpInside) //增加点击事件
        header.sectionBtn.tag = indexPath.section
        return header
    }
}

// MARK: - 单元格的样式
extension ViewController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        let SCREEN_WIDTH = UIScreen.mainScreen().bounds.width
        return CGSizeMake(SCREEN_WIDTH / 4 - 10, SCREEN_WIDTH / 4 - 10) //单个Cell的大小
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        
        return UIEdgeInsetsMake(0, 5, 0, 5) //上、左、下、右距四边的间距
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        
        return CGSizeMake(0, 40)//分组头部视图的高度
    }
}

GroupSection.swift //组头视图

import UIKit

class GroupSection: UICollectionReusableView {
    
    @IBOutlet weak var sectionTitle: UILabel!
    @IBOutlet weak var sectionBtn: UIButton!
}

相关文章

网友评论

  • zxlaikaifa:您好,请问下 点击响应的操作是什么?最近在做OC的项目,希望您能给个参考,点击button展现或者收起行或者列。
  • 24K纯城:collectionView.reloadSections(NSIndexSet(index: sender.tag))
    每次刷新一下,sectionState又会恢复,这样每次要点两次才能展开与收缩!
    6ac693037a59:我也遇见了这个问题
  • 89848af90932:楼主,header的Button的tag在哪里设置的?
  • 8f99f168becd:大神 ,怎么设置第一个默认 展开,其他的是关闭的
    小黑Swift:@wo叫小小虎 设置该个状态为 true
  • 攻城:大神,这个折叠有OC版本的么?
    8f99f168becd:大神,第一个默认是展开的 怎么设置?
    小黑Swift:@攻城 没有喔!思路都是差不多的 :blush:
  • 7bd6414375cf:赞一个

本文标题:iOS开发 - UICollectionView点击展开收起

本文链接:https://www.haomeiwen.com/subject/leerettx.html