https://github.com/potato512/SYSwiftLearning
// MARK: - 数据func setLocalData(){ self.mainArray = NSMutableArray() for number in 1...10 { let rowArray:NSMutableArray = NSMutableArray() for rowNumber in 1...5 { let rowNumberTmp = random() % 1000 + rowNumber rowArray.addobject(String(rowNumberTmp)) } let dict:NSMutableDictionary = NSMutableDictionary() // cell值 dict.setobject(rowArray,forKey: "rowCell") // 页眉,页脚标题 let numberTmp = random() % 1000 + number dict.setobject(String(format: "headerTitle:%@",arguments: [String(numberTmp)]),forKey: "rowheader") dict.setobject(String(format: "footerTitle:%@",forKey: "rowFooter") // cell展开,或收起来状态 dict.setobject(((number % 2 == 0) ? true : false),forKey: "rowStatus") self.mainArray.addobject(dict) }}
// MARK: - 视图func setUI(){ // 列表样式分plain,grouped,其中grouped在列表有页眉,页脚时会随cell滚动,而plain则是先固定后滚动 self.maintableVIEw = UItableVIEw(frame: self.vIEw.bounds,style: UItableVIEwStyle.Grouped) self.vIEw.addSubvIEw(self.maintableVIEw) self.maintableVIEw.backgroundcolor = UIcolor.clearcolor() self.maintableVIEw.delegate = self self.maintableVIEw.dataSource = self self.maintableVIEw.autoresizingMask = UIVIEwautoresizing.FlexibleHeight self.maintableVIEw.tableFooterVIEw = UIVIEw()}
// MARK: - statusClickfunc statusClick(button:UIbutton){ let section = button.tag let dict:NSMutableDictionary! = self.mainArray[section] as! NSMutableDictionary let status:Bool! = dict.objectForKey("rowStatus") as! Bool dict.setobject((status.boolValue ? false : true),forKey: "rowStatus") self.mainArray.replaceObjectAtIndex(section,withObject: dict) self.maintableVIEw.reloadSections(NSIndexSet(index: section),withRowAnimation: .None)}
// MARK: - UItableVIEwDataSource,UItableVIEwDelegate // 分组func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return self.mainArray.count} // MARK: 页眉视图func tableVIEw(tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat { return 40.0} // func tableVIEw(tableVIEw: UItableVIEw,TitleForheaderInSection section: Int) -> String? {// // let dict:NSDictionary! = self.mainArray[section] as! NSDictionary// let Title:String! = dict.objectForKey("rowheader") as! String// // return Title// } // 自定义页眉(注意:自定义时,高度与代理方法中的高度一致,同时代理方法中的标题失效)func tableVIEw(tableVIEw: UItableVIEw,vIEwForheaderInSection section: Int) -> UIVIEw? { let vIEw = UIbutton(frame: CGRectMake(0.0,0.0,CGRectGetWIDth(self.maintableVIEw.frame),40.0)) vIEw.backgroundcolor = UIcolor.greencolor() vIEw.contentHorizontalAlignment = .Center vIEw.setTitlecolor(UIcolor.blackcolor(),forState: .normal) vIEw.setTitlecolor(UIcolor.redcolor(),forState: .Highlighted) // 响应事件,用于修改cell显示状态,即打开,或收起来 vIEw.tag = section vIEw.addTarget(self,action: Selector("statusClick:"),forControlEvents: .touchUpInsIDe) let dict:NSDictionary! = self.mainArray[section] as! NSDictionary let Title:String! = dict.objectForKey("rowheader") as! String vIEw.setTitle(Title,forState: .normal) return vIEw} // MARK: 页脚视图func tableVIEw(tableVIEw: UItableVIEw,heightForFooterInSection section: Int) -> CGfloat { return 20.0} // func tableVIEw(tableVIEw: UItableVIEw,TitleForFooterInSection section: Int) -> String? {// // let dict:NSDictionary! = self.mainArray[section] as! NSDictionary// let Title:String! = dict.objectForKey("rowFooter") as! String// // return Title// } // 自定义页脚(注意:自定义时,高度与代理方法中的高度一致,同时代理方法中的标题失效)func tableVIEw(tableVIEw: UItableVIEw,vIEwForFooterInSection section: Int) -> UIVIEw? { let vIEw = UILabel(frame: CGRectMake(0.0,40.0)) vIEw.backgroundcolor = UIcolor.yellowcolor() vIEw.textAlignment = .Center let dict:NSDictionary! = self.mainArray[section] as! NSDictionary let Title:String! = dict.objectForKey("rowFooter") as! String vIEw.text = Title return vIEw} // MARK: cell单元格func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { let dict:NSDictionary! = self.mainArray[section] as! NSDictionary let status:Bool! = dict.objectForKey("rowStatus") as! Bool if status.boolValue { let array:NSArray! = dict.objectForKey("rowCell") as! NSArray return array.count } return 0} func tableVIEw(tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGfloat { return 60.0} func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { var cell:UItableVIEwCell! = tableVIEw.dequeueReusableCellWithIDentifIEr("UItableVIEwCell") if cell == nil { cell = UItableVIEwCell(style: UItableVIEwCellStyle.SubTitle,reuseIDentifIEr: "UItableVIEwCell") cell.textLabel!.adjustsFontSizetoFitWIDth = true cell.textLabel!.Font = UIFont.systemFontOfSize(12.0) cell.detailTextLabel!.Font = UIFont.systemFontOfSize(20.0) cell.detailTextLabel!.textcolor = UIcolor.redcolor() } let text = String(format: "%@",arguments: [indexPath]) cell.textLabel!.text = text let dict:NSDictionary! = self.mainArray[indexPath.section] as! NSDictionary let array:NSArray! = dict.objectForKey("rowCell") as! NSArray let Title:String! = array.objectAtIndex(indexPath.row) as! String cell.detailTextLabel!.text = Title return cell} func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { tableVIEw.deselectRowAtIndexPath(indexPath,animated: true)}总结
以上是内存溢出为你收集整理的swift中UITableView的使用(分组功能,类似于QQ群组)全部内容,希望文章能够帮你解决swift中UITableView的使用(分组功能,类似于QQ群组)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)