5, Swift tableView点击收起展开的效果

这个是通过OC转换过来的 当时写的时候Swift版本是3.0

原理是标记把每个section的标记全部赋值为0,然后在section中定义同样大小的按钮 ,通过方法来改变section的标记,根据标记来刷新对应的section,即可刷新出数据。

代码如下

import UIKit

class ExpandController: UIViewController,UITableViewDelegate,UITableViewDataSource{
    var titleString = ["姓  名","身份证","手机号"]
    var emptyArray:NSMutableArray?
    override func viewDidLoad() {
        super.viewDidLoad()
        emptyArray = NSMutableArray.init();
        for _ in 0..<titleString.count {
            emptyArray?.add("0");
        }
        self.view.addSubview(self.table);
        // Do any additional setup after loading the view.
        
    }
    // 懒加载tableview
    lazy var table : UITableView = self.tableView()
    func tableView() -> UITableView {
        self.table = UITableView(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: UITableViewStyle.grouped);
        self.table.delegate = self;
        self.table.dataSource = self;
        
        return table;
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3;
    }
    // 1.2 返回行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (emptyArray!.object(at: section) as AnyObject).isEqual("0"){
            return 0;
        }else{
            return 1;//此处返回的是1 故此所有展示的都是1,项目中根据数据改变此处的数据即可
        }
    }
    //1.3 返回行高
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
               return 44;
    }
    //1.4每组的头部高度
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 44;
        
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let button = UIButton();
        button.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44);
        button .titleLabel?.font = UIFont.systemFont(ofSize: 16);
        button .setTitleColor(UIColor .red, for: UIControlState.normal);
        button.tag = section + 1;
        button .backgroundColor = UIColor.orange;
        button.addTarget(self, action: #selector(btnClike), for: UIControlEvents.touchUpInside);
        
        let imageView = UIImageView();
        imageView.frame = CGRect.init(x: 15, y: 12, width: 20, height: 20)
        if (emptyArray!.object(at: section) as AnyObject).isEqual("0"){
            imageView.image = UIImage.init(named: "setting");
        }else if(emptyArray!.object(at: section) as AnyObject).isEqual("1"){
            imageView.image = UIImage.init(named: "财富_select");
        }
        button.addSubview(imageView);
        return button;
    }
    //1.5每组的底部高度
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 1;
        
    }
    //底部视图的定义加载
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return nil;
    }
    //1.6 返回数据源
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
            let identifier = "identtifier";
            var cell = tableView.dequeueReusableCell(withIdentifier: identifier);
            if(cell == nil){
                cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: identifier);
            }
            cell?.textLabel?.text = titleString[indexPath.row];
            return cell!;
    }

    
    /******/
   
    func btnClike(_ btn:UIButton) {
        if (emptyArray!.object(at: btn.tag - 1) as AnyObject).isEqual("0"){
            emptyArray?[btn.tag - 1] = "1";

        }else if(emptyArray!.object(at: btn.tag - 1) as AnyObject).isEqual("1"){
            emptyArray?[btn.tag - 1] = "0";
        }
        self.table.reloadSections(NSIndexSet(index:btn.tag - 1) as IndexSet, with: UITableViewRowAnimation.automatic);
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值