iOS開發——UI_swift篇&UITableView實現儲存格展開與隱藏

來源:互聯網
上載者:User

標籤:

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實現儲存格展開與隱藏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.