【IOS】Swift编写可折叠的tableView

来源:互联网 发布:apache ant 1.9.3下载 编辑:程序博客网 时间:2024/05/21 10:43

1.项目说明

本项目使用Swift2.3实现可折叠的tableView,具体的思路是通过在section上添加监听函数,当用户点击某一section时,将其状态由0更改为1。再判断section内的cell条数的函数中进行判断,若状态为0,则返回0。若状态为1,则返回对应的cell条数。最后不要忘记重载tableView。

2.效果展示

初始界面

这里写图片描述

3.代码实现

//第一步 定义一个字典dict,并进行初始化 var dict:NSMutableDictionary=[ : ] //这里假设我们有5个section for (i=0;i<5;i++){      dict.setValue(0, forKey: "\(i)" )     }
//第二步,构造监听函数 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{ //新建一个View,可以通过设置View来设置section的样式        let view:UIView=UIView(frame:CGRect(x: 0,y: 0,width: UIScreen.mainScreen().bounds.width,height: 58))        view.backgroundColor=UIColor(red: 240/255,green:240/255,blue:240/255,alpha:1) //新建Label用来放置section的内容        let label=UILabel(frame:CGRect(x: 20,y: 10,width: 300,height: 40)) //设置View的样式        view.layer.borderColor=UIColor.darkGrayColor().CGColor        view.layer.borderWidth=0.4        label.text=self.atlas.adHeaders![section]        view.addSubview(label) //将用户点击的section对应的index赋给View的tag属性        view.tag=section//新建recognizer并设置响应函数,该函数将会在下一步实现        let recognizer=UITapGestureRecognizer(target: self, action: #selector(LeftView.display(_:))) //不要忘记将其添加到view当中        view.addGestureRecognizer(recognizer)        return view    }
//第三步,实现上一步的响应函数 func display(g:UITapGestureRecognizer){        let key:String = "\(g.view!.tag)"        let ss:Int=self.dict.objectForKey(key) as! Int        if(ss==0){        //将key为对应section的value设为1,表示已经被点击            self.dict.setValue(1, forKey:"\(g.view!.tag)")            print(dict["\(g.view!.tag)"])        }        else{            self.dict.setValue(0, forKey:"\(g.view!.tag)")        }        //重载section,并设置动画。千万不要忘记reload!!        let indexSet:NSIndexSet=NSIndexSet.init(index: g.view!.tag)        tableView.reloadSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)    }
//第四步,实现返回控件行数的函数 //返回表格行数(也就是返回控件数)    func tableView(_tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        let data = [1,2,3,4,5]        let ss=self.dict["\(section)"] as! Int        //若值为1,则返回section内的cell个数        if(ss==1){            return data!.count        }        //否则,返回0        else{            return 0        }    }
//第五步,完成tableView的构建 //设置分区个数    func numberOfSectionsInTableView(tableView: UITableView) -> Int    {        return 2}    // 决定指定分区的头部    func tableView(_tableView: UITableView, titleForHeaderInSection section: Int)        -> String? {            return "section的名称"    }    //创建各单元显示内容(创建参数indexPath指定的单元)    func tableView(_tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)        -> UITableViewCell {            let identify:String="SwiftCell"            var cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)       if cell.isEqual(nil){              cell=TableCell(style:UITableViewCellStyle.Default,reuseIdentifier: identify)            }            cell.accessoryType = .DetailDisclosureButton            let secno = indexPath.section            var data = [1,2,3,4,5]            cell.backgroundColor=UIColor.clearColor()            cell.textLabel?.text = data![indexPath.row]            return cell             }    // UITableViewDelegate 方法,处理列表项的选中事件    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)    }

4.注意事项

  • 1.在给字典的key赋予键值的时候,由于g.view?.tag是一个可选型,因此将该可选型赋给dict键值的时候就会出问题,因此在这里使用了强制解包后,即使用g.view!.tag,再赋值。
  • 2.在写样式的时候,若想改变section的高度,直接修改View是行不通的,要采用如下写法:
 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{         return 60            }
  • 3.一定不要忘记reload整个tableView
1 0
原创粉丝点击