1 import UIKit
2
3 class ViewController: UIViewController,UITableViewDelegate,
4 UITableViewDataSource {
5
6 var tableView:UITableView?
7
8 var ctrlnames:[String] = ["UILabel 标签","UIButton 按钮","UIDatePiker 日期选择器",
9 "UITableView 表格视图"]
10
11 var selectedCellIndexPath:NSIndexPath!
12
13 override func viewDidLoad() {
14 super.viewDidLoad()
15
16 //创建表视图
17 self.tableView = UITableView(frame: UIScreen.mainScreen().applicationFrame,
18 style:UITableViewStyle.Plain)
19 self.tableView!.delegate = self
20 self.tableView!.dataSource = self
21 //创建一个重用的单元格
22 self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
23 self.view.addSubview(self.tableView!)
24 }
25
26 //在本例中,只有一个分区
27 func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
28 return 1;
29 }
30
31 //返回表格行数(也就是返回控件数)
32 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
33 return self.ctrlnames.count
34 }
35
36 //创建各单元显示内容(创建参数indexPath指定的单元)
37 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
38 -> UITableViewCell
39 {
40 var label = UILabel(frame:CGRectZero)
41 label.setTranslatesAutoresizingMaskIntoConstraints(false)
42 label.text = self.ctrlnames[indexPath.row]
43
44 var textview=UITextView(frame:CGRectZero)
45 textview.setTranslatesAutoresizingMaskIntoConstraints(false)
46 textview.textColor = UIColor.grayColor()
47 //演示效果,暂时写死
48 textview.text = "UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,"
49
50 let identify:String = "SwiftCell"
51 var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:identify)
52 //自动遮罩不可见区域,超出的不显示
53 cell.layer.masksToBounds = true
54 cell.contentView.addSubview(label)
55 cell.contentView.addSubview(textview)
56
57 //创建一个控件数组
58 var views:NSMutableDictiOnary= NSMutableDictionary()
59 views.setValue(label, forKey: "label")
60 views.setValue(textview, forKey: "textview")
61 cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
62 "H:|-15-[label]-15-|", options: nil, metrics: nil, views: views))
63 cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
64 "H:|-15-[textview]-15-|", options: nil, metrics: nil, views: views))
65 cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
66 "V:|[label(40)]", options: nil, metrics: nil, views: views))
67 cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
68 "V:|-40-[textview(80)]", options: nil, metrics: nil, views: views))
69 return cell
70 }
71
72 // UITableViewDelegate 方法,处理列表项的选中事件
73 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
74 {
75 self.tableView!.deselectRowAtIndexPath(indexPath, animated: false)
76 selectedCellIndexPath = indexPath
77 // Forces the table view to call heightForRowAtIndexPath
78 tableView!.reloadRowsAtIndexPaths([indexPath],
79 withRowAnimation: UITableViewRowAnimation.Automatic)
80 }
81
82 //点击单元格会引起cell高度的变化,所以要重新设置
83 func tableView(tableView: UITableView,
84 heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
85 if(selectedCellIndexPath != nil && selectedCellIndexPath == indexPath){
86 return 120
87 }
88 return 40
89 }
90 }