UItableView分组功能
class UITableViewControllerGroup: UIViewController, UITableViewDataSource, UITableViewDelegate { var titleString:String! @IBOutlet var titleLabel:UILabel! @IBOutlet var listTableView : UITableView! //索引字母数组 var arrayOfCharacters:[String] = ["A","B","C","D","E","F","G"] var wordLibrary:Dictionary <String, [String]> = [:] //定义数组 var items:[String] = ["UITextView", "UISegmentedControl", "UISlider", "UISwitch", "UIActivityIndicatorView", "UIProgressView", "UIPageControl", "UIStepper", "UIImageView", "UIWebView", "UIScrollView"] //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(true) } override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString //创建6个数组,6个组使用 var a_Word:[String] = ["aim","air","aircraft","airplane","aloft","alphabetically"] var b_Word:[String] = ["baa","babble","back","baldric","ballooning","bandbook","bandy","bankruptcy","baptistery"] var c_Word:[String] = ["cablegram","cacti","cacophonous","cairn","calcimine","calculus","caldron","candelabrum","candlestick","canker"] var d_Word:[String] = ["dagger","daguerreotype","dame","damnation","dapper","daredevil","darwin","dateless","daw","date","deacon"] var e_Word:[String] = ["eagerly","early","earn","earnestness","earthworm","eastbound","east","easel","earthwork","ease","easterly","eastern"] var f_Word:[String] = ["face","facial","facilitate","fact","faction","facilities","facing","facsimile","facet","factitious","facility","factor","factorage"] var g_Word:[String] = ["gazetteer","generalization","geneva","gibbet","glasshouse","gnarl","goad","going","grace","gradual","grammar","grandiose","grandstand","granulate"] //将数组添加到字典里面 wordLibrary.updateValue(a_Word, forKey: "A") wordLibrary.updateValue(b_Word, forKey: "B") wordLibrary.updateValue(c_Word, forKey: "C") wordLibrary.updateValue(d_Word, forKey: "D") wordLibrary.updateValue(e_Word, forKey: "E") wordLibrary.updateValue(f_Word, forKey: "F") wordLibrary.updateValue(g_Word, forKey: "G") //给TableView添加表头页眉 var headerView:UIView = UIView(frame: CGRectMake(, , listTableView.frame.size.width, )) var headerlabel:UILabel = UILabel(frame: headerView.bounds) headerlabel.textColor = UIColor.blackColor() headerlabel.backgroundColor = UIColor.clearColor() headerlabel.font = UIFont.systemFontOfSize() headerlabel.text = "TabelView 页尾表头 页眉" headerView.addSubview(headerlabel) headerView.backgroundColor = UIColor.redColor() listTableView.tableHeaderView = headerView //给TabelView添加页尾 var footerView:UIView = UIView(frame: CGRectMake(, , listTableView.frame.size.width, )) var footerlabel:UILabel = UILabel(frame: footerView.bounds) footerlabel.textColor = UIColor.blackColor() footerlabel.backgroundColor = UIColor.clearColor() footerlabel.font = UIFont.systemFontOfSize() footerlabel.text = "TabelView 页尾视图" footerView.addSubview(footerlabel) footerView.backgroundColor = UIColor.greenColor() listTableView.tableFooterView = footerView //刷新TableView listTableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ //MARK: - UITableViewDelegate //tableView数据源:返回几节(组) func numberOfSectionsInTableView(tableView: UITableView) -> Int { return arrayOfCharacters.count } //tableView数据源:返回每组行数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var key = arrayOfCharacters[section] as String var tempWord:[String]! = wordLibrary[key] return tempWord.count } //tableView 数据源:每一行高度 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { } //tableView数据源:每一行内容 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //Cell标示符,代表一系列 // OC:使用static, swift:使用let let cellIdentifier: String = "cellIdentifier" //通过cellIdentifier标示符取没有使用的Cell //有可能不存在,所以使用:optional var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell //如果cell取到是空 if cell == nil { // no value //创建新的cell //cell样式:UITableViewCellStyle.Default //cell标示符:cellIdentifier cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier) //设置字体 // cell!.textLabel.font = UIFont.systemFontOfSize(14) //2015年4月10号修改 cell!.textLabel?.font = UIFont.systemFontOfSize() //设置选中cell样式 cell!.selectionStyle = .Gray; //设置cell后面箭头样式 cell!.accessoryType = .DisclosureIndicator; } var key = arrayOfCharacters[indexPath.section] as String var tempWord:[String]! = wordLibrary[key] //从数组取对应值给cell赋值 // cell!.textLabel.text = tempWord[indexPath.row] //2015年4月10号修改 cell!.textLabel?.text = tempWord[indexPath.row] return cell!; } //每一组的头部,添加一个视图 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { var view:UIView = UIView(frame: CGRectZero) view.backgroundColor = UIColor(red: 0xf0/255.0, green: 0xf0/255.0 , blue: 0xf6/255.0 , alpha: 1.0) var label:UILabel = UILabel(frame: CGRectMake(, , , )) label.textColor = UIColor(red: 0x8f/255.0, green: 0x8f/255.0, blue: 0x94/255.0, alpha: 1.0) label.backgroundColor = UIColor.clearColor() label.font = UIFont.systemFontOfSize() view.addSubview(label) label.text = arrayOfCharacters[section] return view; } //每一组的末端,添加一个视图 func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { var view:UIView = UIView(frame: CGRectZero) view.backgroundColor = UIColor.orangeColor() var label:UILabel = UILabel(frame: CGRectMake(, , , )) label.textColor = UIColor.blackColor() label.backgroundColor = UIColor.clearColor() label.font = UIFont.systemFontOfSize() view.addSubview(label) label.text = "第\(section) 组结束,本组页脚视图" return view; } func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! { return arrayOfCharacters //返回索引数据源 } //点击索引,移动TableView的组位置 func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { var tpIndex:NSInteger = //便利索引值 for character in arrayOfCharacters { //判断索引值 和 组名称相等,返回组坐标 if character == title { return tpIndex } tpIndex++ } } //tableView代理:点击一行 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { //释放选中效果 tableView.deselectRowAtIndexPath(indexPath, animated: true) }