【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
- 【IOS】Swift编写可折叠的tableView
- 实现可折叠的分组tableview
- iOS TableView 使用(swift)
- ios swift tableview
- IOS swift 自定义Tableview cell
- swift- tableView 的使用
- 今天要用到可折叠的tableview 折腾了半天
- iOS-swift-tableview隐藏多余的cell分割线
- IOS(swift)-scrollView(tableView) ·图片加载逻辑的优化
- ios TableView那些事3(Swift 三) TableView 的删除操作
- swift中tableview的使用
- 利用swift写的tableView
- swift tableView的简单用法
- ios TableView那些事2(Swift 一) 初见TableView
- ios TableView那些事2(Swift 二) 初见TableView Grouped
- iOS swift 自定义tableView不显示cell
- iOS 11 Swift-导航&状态栏&tableview适配
- iOS开发swift中tableView的条目点击的背景和文字颜色
- VS2017新特性
- Spring学习心得(5)-- spring容器创建对象的生命周期
- win10 应用商店无法打开
- nginx.conf配置参数详解
- Swift 懒加载
- 【IOS】Swift编写可折叠的tableView
- Python 读取文件替换其中的内容并输出到一个新的文件内
- SVNkit-SVNAuthenticationException
- FMDB的简单使用
- linux下卸载oracle
- CSS三大特性
- java junit测试quartz job报错
- 模仿读取struts.xml TDD重写
- 开通大吉