码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能

时间:2015-06-07 23:18:24      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

UItableView分组功能

 

  1 class UITableViewControllerGroup: UIViewController, UITableViewDataSource, UITableViewDelegate  {
  2     
  3     var titleString:String!
  4     
  5     @IBOutlet var titleLabel:UILabel!
  6     @IBOutlet var listTableView : UITableView!
  7     
  8     
  9     //索引字母数组
 10     var arrayOfCharacters:[String] = ["A","B","C","D","E","F","G"]
 11     
 12     
 13     var wordLibrary:Dictionary <String, [String]> = [:]
 14     
 15     
 16     
 17     
 18     //定义数组
 19     var items:[String] = ["UITextView",
 20         "UISegmentedControl",
 21         "UISlider",
 22         "UISwitch",
 23         "UIActivityIndicatorView",
 24         "UIProgressView",
 25         "UIPageControl",
 26         "UIStepper",
 27         "UIImageView",
 28         "UIWebView",
 29         "UIScrollView"]
 30     
 31     
 32     //返回按钮事件
 33     @IBAction func backButtonClick()
 34     {
 35         self.navigationController?.popViewControllerAnimated(true)
 36     }
 37     
 38     
 39     override func viewDidLoad() {
 40         super.viewDidLoad()
 41         
 42         titleLabel.text = titleString
 43         
 44         
 45         
 46         //创建6个数组,6个组使用
 47         var a_Word:[String] = ["aim","air","aircraft","airplane","aloft","alphabetically"]
 48         var b_Word:[String] = ["baa","babble","back","baldric","ballooning","bandbook","bandy","bankruptcy","baptistery"]
 49         var c_Word:[String] = ["cablegram","cacti","cacophonous","cairn","calcimine","calculus","caldron","candelabrum","candlestick","canker"]
 50         var d_Word:[String] = ["dagger","daguerreotype","dame","damnation","dapper","daredevil","darwin","dateless","daw","date","deacon"]
 51         var e_Word:[String] = ["eagerly","early","earn","earnestness","earthworm","eastbound","east","easel","earthwork","ease","easterly","eastern"]
 52         var f_Word:[String] = ["face","facial","facilitate","fact","faction","facilities","facing","facsimile","facet","factitious","facility","factor","factorage"]
 53         var g_Word:[String] = ["gazetteer","generalization","geneva","gibbet","glasshouse","gnarl","goad","going","grace","gradual","grammar","grandiose","grandstand","granulate"]
 54         
 55         
 56         //将数组添加到字典里面
 57         wordLibrary.updateValue(a_Word, forKey: "A")
 58         wordLibrary.updateValue(b_Word, forKey: "B")
 59         wordLibrary.updateValue(c_Word, forKey: "C")
 60         wordLibrary.updateValue(d_Word, forKey: "D")
 61         wordLibrary.updateValue(e_Word, forKey: "E")
 62         wordLibrary.updateValue(f_Word, forKey: "F")
 63         wordLibrary.updateValue(g_Word, forKey: "G")
 64 
 65         
 66         //给TableView添加表头页眉
 67         var headerView:UIView = UIView(frame: CGRectMake(0, 0, listTableView.frame.size.width, 60))
 68         var headerlabel:UILabel = UILabel(frame: headerView.bounds)
 69         headerlabel.textColor = UIColor.blackColor()
 70         headerlabel.backgroundColor = UIColor.clearColor()
 71         headerlabel.font = UIFont.systemFontOfSize(16)
 72         headerlabel.text = "TabelView 页尾表头 页眉"
 73         headerView.addSubview(headerlabel)
 74         headerView.backgroundColor = UIColor.redColor()
 75         listTableView.tableHeaderView = headerView
 76         
 77         
 78         //给TabelView添加页尾
 79         var footerView:UIView =  UIView(frame: CGRectMake(0, 0, listTableView.frame.size.width, 60))
 80         var footerlabel:UILabel = UILabel(frame: footerView.bounds)
 81         footerlabel.textColor = UIColor.blackColor()
 82         footerlabel.backgroundColor = UIColor.clearColor()
 83         footerlabel.font = UIFont.systemFontOfSize(16)
 84         footerlabel.text = "TabelView 页尾视图"
 85         footerView.addSubview(footerlabel)
 86         footerView.backgroundColor = UIColor.greenColor()
 87         listTableView.tableFooterView = footerView
 88         
 89         
 90         
 91         //刷新TableView
 92         listTableView.reloadData()
 93     }
 94 
 95     override func didReceiveMemoryWarning() {
 96         super.didReceiveMemoryWarning()
 97         // Dispose of any resources that can be recreated.
 98     }
 99     
100 
101     /*
102     // MARK: - Navigation
103 
104     // In a storyboard-based application, you will often want to do a little preparation before navigation
105     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
106         // Get the new view controller using segue.destinationViewController.
107         // Pass the selected object to the new view controller.
108     }
109     */
110 
111     
112     //MARK: - UITableViewDelegate
113     
114     //tableView数据源:返回几节(组)
115     func numberOfSectionsInTableView(tableView: UITableView) -> Int
116     {
117         return arrayOfCharacters.count
118     }
119     
120     //tableView数据源:返回每组行数
121     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
122     {
123         var key = arrayOfCharacters[section] as String
124         var tempWord:[String]! = wordLibrary[key]
125         return tempWord.count
126     }
127     
128     //tableView 数据源:每一行高度
129     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
130     {
131         return 50
132     }
133     
134     //tableView数据源:每一行内容
135     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
136     {
137         //Cell标示符,代表一系列
138         // OC:使用static,  swift:使用let
139         let cellIdentifier: String = "cellIdentifier"
140         
141         //通过cellIdentifier标示符取没有使用的Cell
142         //有可能不存在,所以使用:optional
143         var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
144         
145         //如果cell取到是空
146         if cell == nil { // no value
147             
148             //创建新的cell
149             //cell样式:UITableViewCellStyle.Default
150             //cell标示符:cellIdentifier
151             cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
152             
153             //设置字体
154 //            cell!.textLabel.font = UIFont.systemFontOfSize(14)
155             //2015年4月10号修改
156             cell!.textLabel?.font = UIFont.systemFontOfSize(14)
157             
158             //设置选中cell样式
159             cell!.selectionStyle = .Gray;
160             
161             //设置cell后面箭头样式
162             cell!.accessoryType = .DisclosureIndicator;
163         }
164         
165         var key = arrayOfCharacters[indexPath.section] as String
166         var tempWord:[String]! = wordLibrary[key]
167         
168         //从数组取对应值给cell赋值
169 //        cell!.textLabel.text = tempWord[indexPath.row]
170         //2015年4月10号修改
171         cell!.textLabel?.text = tempWord[indexPath.row]
172         
173         return cell!;
174     }
175     
176     
177     //每一组的头部,添加一个视图
178     func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
179     {
180         var view:UIView = UIView(frame: CGRectZero)
181         view.backgroundColor = UIColor(red: 0xf0/255.0, green: 0xf0/255.0 , blue: 0xf6/255.0 , alpha: 1.0)
182         
183         var label:UILabel = UILabel(frame: CGRectMake(20, 0, 100, 20))
184         label.textColor = UIColor(red: 0x8f/255.0, green: 0x8f/255.0, blue: 0x94/255.0, alpha: 1.0)
185         label.backgroundColor = UIColor.clearColor()
186         label.font = UIFont.systemFontOfSize(16)
187         view.addSubview(label)
188         
189         label.text = arrayOfCharacters[section]
190         
191         return view;
192     }
193     
194     
195     //每一组的末端,添加一个视图
196     func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
197     {
198         var view:UIView = UIView(frame: CGRectZero)
199         view.backgroundColor = UIColor.orangeColor()
200         
201         var label:UILabel = UILabel(frame: CGRectMake(20, 0, 300, 20))
202         label.textColor = UIColor.blackColor()
203         label.backgroundColor = UIColor.clearColor()
204         label.font = UIFont.systemFontOfSize(16)
205         view.addSubview(label)
206         
207         label.text = "第\(section) 组结束,本组页脚视图"
208         
209         return view;
210     }
211     
212     
213     
214     
215     
216     func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!
217     {
218         return arrayOfCharacters   //返回索引数据源
219     }
220     
221     //点击索引,移动TableView的组位置
222     func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int
223     {
224         var tpIndex:NSInteger = 0
225         
226         //便利索引值
227         for character in arrayOfCharacters
228         {
229             //判断索引值 和 组名称相等,返回组坐标
230             if character == title
231             {
232                 return tpIndex
233             }
234             
235             tpIndex++
236         }
237         
238         return 0
239     }
240     
241     
242     
243     //tableView代理:点击一行
244     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
245     {
246         //释放选中效果
247         tableView.deselectRowAtIndexPath(indexPath, animated: true)
248     }

 

 

iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4559217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!