UITableView implementing cell expansion and hiding
The following is a demo of the collapse spread effect of cell cell. When you click a cell, the cell expands to show some details. Clicking on another cell will turn off the original and animate. The effect is as follows: The code is as follows:
1 Import UIKit2 3 classViewcontroller:uiviewcontroller,uitableviewdelegate,4 Uitableviewdatasource {5 6var tableview:uitableview?7 8var ctrlnames:[string] = ["UILabel Label","UIButton Button","Uidatepiker Date Selector",9 "UITableView Table View"]Ten Onevar selectedcellindexpath:nsindexpath! A - Overridefunc viewdidload () { - super.viewdidload () the - //Create a table view -Self.tableview =UITableView (Frame:UIScreen.mainScreen (). Applicationframe, - Style:UITableViewStyle.Plain) +self.tableview!.Delegate= Self -self.tableview!. DataSource = Self + //to create a reusable cell Aself.tableview!. RegisterClass (Uitableviewcell.self, Forcellreuseidentifier:"Swiftcell") atSelf.view.addSubview (self.tableview!) - } - - //in this case, there is only one partition -Func Numberofsectionsintableview (tableview:uitableview!)Int { - return 1; in } - to //returns the number of table rows (that is, the number of returned controls) +Func TableView (Tableview:uitableview, numberofrowsinsection section:int)Int { - returnSelf.ctrlnames.count the } * $ //Create individual cell display content (create the cell specified by the parameter Indexpath)Panax Notoginseng func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath) --UITableViewCell the { +var label =UILabel (Frame:cgrectzero) ALabel.settranslatesautoresizingmaskintoconstraints (false) theLabel.text =Self.ctrlnames[indexpath.row] + -var textview=Uitextview (Frame:cgrectzero) $Textview.settranslatesautoresizingmaskintoconstraints (false) $Textview.textcolor =Uicolor.graycolor () - //demo effect, temporary write dead -Textview.text ="Uidatepicker is a controller class that encapsulates the uipickerview, but he is a subclass of Uicontrol," the -Let identify:string ="Swiftcell"Wuyivar cell =UITableViewCell (Style:UITableViewCellStyle.Default, reuseidentifier:identify) the //Auto Matte Invisible area, out of display -Cell.layer.masksToBounds =true Wu Cell.contentView.addSubview (label) - Cell.contentView.addSubview (TextView) About $ //creating an array of controls -var views:nsmutabledictionary =nsmutabledictionary () -Views.setvalue (Label, Forkey:"label") -Views.setvalue (TextView, Forkey:"TextView") A cell.contentView.addConstraints (Nslayoutconstraint.constraintswithvisualformat ( + "h:|-15-[label]-15-|", Options:nil, Metrics:nil, views:views)) the cell.contentView.addConstraints (Nslayoutconstraint.constraintswithvisualformat ( - "h:|-15-[textview]-15-|", Options:nil, Metrics:nil, views:views)) $ cell.contentView.addConstraints (Nslayoutconstraint.constraintswithvisualformat ( the "V:|[label (+)]", Options:nil, Metrics:nil, views:views)) the cell.contentView.addConstraints (Nslayoutconstraint.constraintswithvisualformat ( the "V:|-40-[textview (+)]", Options:nil, Metrics:nil, views:views)) the returnCell - } in the //Uitableviewdelegate method, handling selected events for list items theFunc TableView (tableview:uitableview!, Didselectrowatindexpath indexpath:nsindexpath!) About { theself.tableview!. Deselectrowatindexpath (Indexpath, animated:false) theSelectedcellindexpath =Indexpath the //forces the table view to call Heightforrowatindexpath +tableview!. Reloadrowsatindexpaths ([Indexpath], - withRowAnimation:UITableViewRowAnimation.Automatic) the }Bayi the //clicking on a cell will cause the cell height to change, so you need to reset the func TableView (Tableview:uitableview, -Heightforrowatindexpath Indexpath:nsindexpath)CGFloat { - if(Selectedcellindexpath! = Nil && Selectedcellindexpath = =Indexpath) { the return - the } the return + the } -}
iOS Development--ui_swift Chapter &uitableview Implementing cell Expansion and hiding