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)
     }
 

iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能的更多相关文章

  1. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用

    swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...

  2. iOS开发——新特性Swift篇&Swift 2.0 异常处理

    Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. iOS开发——网络编程Swift篇&Alamofire详解

    Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...

  5. ios开发——实用技术总结Swift篇&swift常用开发技术总结

    swift常用开发技术总结 懒加载:属性,数组(字典),控件... 数组(懒加载): lazy var shops:Array<Dictionary<String, String>& ...

  6. iOS开发——网络编程Swift篇&(八)SwiftyJSON详解

    SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...

  7. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  8. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  9. iOS开发零基础--Swift篇 元组

    元组的介绍 元组是Swift中特有的,OC中并没有相关类型 它是什么呢? 它是一种数据结构,在数学中应用广泛 类似于数组或者字典 可以用于定义一组数据 组成元组类型的数据可以称为“元素” 元组的定义 ...

  10. iOS开发零基础--Swift篇 循环

    循环的介绍 在开发中经常会需要循环 常见的循环有:for/while/do while. 这里我们只介绍for/while,因为for/while最常见 for循环的写法 最常规写法 // 传统写法 ...

随机推荐

  1. extern "c" 的作用

    作用:实现C和C++混合编程. 原理:C和C++编译器编译之后,函数名会编译成不同的名字,链接阶段名字查找会找不到目标,后面实例中会详解. 用法:①.c文件中定义的函数,.cpp文件要调用时,该.cp ...

  2. 通知(Toast+Notification)

    Toast简要说明:(前面已经用过好多次了) Toast是一种非持久的(在屏幕上面留一会儿就消失了),提供给用户简洁提示信息的视图. 它不阻断用户的操作,一般用于显示一些不重要的信息.(比方说设置音量 ...

  3. Pregel: A System for Large-Scale Graph Processing(译)

    [说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...

  4. Chapter 3 Start Caffe with MNIST Demo

    先从一个具体的例子来开始Caffe,以MNIST手写数据为例. 1.下载数据 下载mnist到caffe-master\data\mnist文件夹. THE MNIST DATABASE:Yann L ...

  5. UDP广域网,局域网通信-原理分析,穿透技术

    一.UDP局域网通信. 这个比较简单,关于局域网中的2台或者更多的计算机之间的UDP通信,网络上一大把,直接复制粘贴就可以使用,原理也非常简单.所以,本文不做详细介绍. 二.UDP广域通信(包括路由器 ...

  6. T-SQL 批处理

    批处理简介 批处理是作为一个逻辑单元的T-SQL语句.如果一条语句不能通过语法分析,那么不会运行任何语句.如果一条语句在运行时失败,那么产生错误的语句之前的语句都已经运行了. 为了将一个脚本分为多个批 ...

  7. Django 1.6 最佳实践: 如何正确使用 Signal(转)

    原文:http://www.weiguda.com/blog/38/ 如何正确的使用signal: 简单回答是: 在其他方法无法使用的情况下, 才最后考虑使用signal. 因为新的django开发人 ...

  8. iconv 的参数问题

    工作中遇到一个转码的问题,如下代码说话 void encode_convert(iconv_t& cd, const char* str, size_t str_len, std::strin ...

  9. 安装php5.5

    安装php5.5 ./configure --prefix=/usr/local/php5.5.14/ --with-apxs2=/usr/local/apache2.2.27/bin/apxs -- ...

  10. python 加密解密

    1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...