美文网首页
swift简易单元格折叠展开功能

swift简易单元格折叠展开功能

作者: 丨涓涓 | 来源:发表于2019-03-11 14:59 被阅读0次

无所事事的时候就用swift搭建简单界面,今天写了简易单元格折叠展开的功能,适合swift初学者,当时我也是个swift初学者。效果如下图:


AB95B177-122A-4DD3-A03F-EA8ED5253A3C.png

大概思路就是,一个存放bool类型的数组,数组对应组的头视图,通过isOpen判断折叠还是展开。具体实现代码如下:

//
//  OtherViewController.swift
//  swiftDemo
//
//  Created by apple on 2019/3/11.
//  Copyright © 2019年 apple. All rights reserved.
//

import UIKit

class OtherViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var tableView:UITableView?
    var sectionArray:NSMutableArray?
    var currentSection:NSInteger?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let backBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 30, height: 30));
        backBtn.setTitle("返回", for: .normal);
        backBtn.setTitleColor(UIColor.black, for: .normal);
        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15);
        backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: backBtn);
        
        sectionArray = NSMutableArray.init();
        for i in 1...10 {
            let isOpen = false;
            sectionArray?.add(isOpen);
            print(i);
        }
        
        tableView = UITableView.init(frame: self.view.frame, style: .plain);
        tableView?.delegate = self;
        tableView?.dataSource = self;
        tableView?.separatorStyle = .none;
        tableView?.estimatedSectionFooterHeight = 0;
        tableView?.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "sectionCell")
        self.view.addSubview(tableView!);
        
    }
    
    @objc func back(){
        self.dismiss(animated: true, completion: nil);
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return (sectionArray?.count)!;
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        return 5;
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView();
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50;
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 50));
        headView.backgroundColor = UIColor.lightGray;
        let btn = UIButton.init(frame: CGRect.init(x: 15, y: 0, width: self.view.frame.size.width - 30, height: 50));
        btn.setTitle("第\(section)组", for: .normal);
        btn.contentHorizontalAlignment = .left;
        btn.tag = section;
        btn.addTarget(self, action: #selector(reloadData(sender:)), for: .touchUpInside);
        headView.addSubview(btn);
        return headView;
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let isOpen = sectionArray![indexPath.section] as! Bool;
        if isOpen {
            return 50;
        }
        return 0;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "sectionCell");
        cell?.textLabel?.text = "第\(indexPath.section)组第\(indexPath.row)个";
        cell?.textLabel?.textColor = UIColor.lightGray;
        cell?.clipsToBounds = true;
        return cell!;
    }
    
    
    @objc func reloadData(sender:UIButton){
        let isOpen = sectionArray![sender.tag] as! Bool;
        sectionArray?.replaceObject(at: sender.tag, with: !isOpen);
        tableView?.reloadData();
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

相关文章

  • swift简易单元格折叠展开功能

    无所事事的时候就用swift搭建简单界面,今天写了简易单元格折叠展开的功能,适合swift初学者,当时我也是个sw...

  • (iOS)商城筛选

    商城筛选的功能的Demo,实现如下功能:【CollectionView】初步展示数据,Section展开/折叠,以...

  • 前端数据列表展开折叠功能

    思路1.定义一个数组来存放所有列表的状态值var recFlag2.在列表对象中根据该对象的长度循环放入每个列表的...

  • 折叠展开

    早上阅读了最近很火的《北京折叠》,文章不长,很快读完,中间有很多的描写还是跳过的,因为不是精读,因此很多细节并不清...

  • Swift-单元格展开收起

    UITableViewCell单元格点击收起比较常见,简单的看一下效果图: 通过Section的状态来来控制Sec...

  • Flutter代码的一些快捷键

    折叠部分代码: ⌘ + -展开部分代码: ⌘ + +折叠全部代码: ⌘ + ⇧ + -展开全部代码: ⌘ + ⇧ ...

  • spacemacs常用操作

    折叠 zc 折叠当前块(函数)zm 折叠当前文件的所有函数zo 展开当前折叠zr 展开当前文件的所有折叠 函数跳转...

  • iOS tableViewCell折叠效果实现(附代码)

    对项目用到的一个tabelViewcell折叠效果的小功能,总结如下: 1、展开前,如下图 2、展开后 实现思想及...

  • ExpandableListView的使用指南

    ExpandableListView可以实现二级列表功能,可以展开,折叠,用例如下 adapter代码: 其中标题...

  • 干货!Swift/OC简单的MVVM模式

    MVVM模式,废话不多上代码: Swift——基础简易版本 Swift——RxSwift进阶 OC——基础简易版本...

网友评论

      本文标题:swift简易单元格折叠展开功能

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