標籤:
UITableView實現儲存格展開與隱藏
下面是一個列表儲存格cell的摺疊展開效果的demo。當點擊儲存格時會展開該儲存格,便於顯示一些詳情什麼的。點擊其他儲存格原來的會關閉,同時有動畫效果。 效果如如下: 代碼如下:
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 = self20 self.tableView!.dataSource = self21 //建立一個重用的儲存格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.count34 }35 36 //建立各單元顯示內容(建立參數indexPath指定的單元)37 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)38 -> UITableViewCell39 {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 = true54 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 cell70 }71 72 // UITableViewDelegate 方法,處理清單項目的選中事件73 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)74 {75 self.tableView!.deselectRowAtIndexPath(indexPath, animated: false)76 selectedCellIndexPath = indexPath77 // Forces the table view to call heightForRowAtIndexPath78 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 12087 }88 return 4089 }90 }
iOS開發——UI_swift篇&UITableView實現儲存格展開與隱藏