Table View Memo
This article will be to forget the main, mainly some basic agent method and data source method specific optimization Good idea will follow up.
data source methods for Table viewdata source methods that must be implemented
Returns the cell for each row, which can be cached for processing, and may also cause a reuse problem. func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell { TableView and cell are all let cell = Tableview.dequeuereusablecellwithidentifier (identifier, storyboard) dragged up Forindexpath:indexpath) Cell.textlabel? Text = datas[indexpath.row].0 Cell.detailtextlabel?. Text = datas[indexpath.row].1 return cell } //Tell TableView should have how many rows func tableView (tableView: UITableView, Numberofrowsinsection section:int), Int { return datas.count }
methods of the group
Tell TableView how many groups, the default is 1 func numberofsectionsintableview (tableview:uitableview), Int { return 1 } //Tell TableView the text that should be displayed at the end of the group, even if the height of the group footer is not set. func TableView (Tableview:uitableview, Titleforfooterinsection section:int), String? { Return "This is the end of the group" } //Tell TableView that the group header should display the text, even if the height of the group header is not set also applies. func TableView (Tableview:uitableview, Titleforheaderinsection section:int), String? { Return "This is the group header" }
How to edit rowDelete:
Whether a row can be edited, but only setting this method is invalid func tableView (Tableview:uitableview, Caneditrowatindexpath Indexpath:nsindexpath ), Bool { return True } //In the case of editable, by judging the type of editing, the implementation of specific logic, to achieve true editable. func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath indexpath:nsindexpath) { if Editingstyle = = Uitableviewcelleditingstyle.delete { Datas.removeatindex (Indexpath.row) tableview.deleterowsatindexpaths ([Indexpath], withrowanimation:. Fade) } }
Add:
This is the proxy method, which returns the edit Type func tableView (Tableview:uitableview, Editingstyleforrowatindexpath Indexpath:nsindexpath)- > Uitableviewcelleditingstyle { return. Insert } //A row is editable, but setting this method is not valid for func tableView (Tableview:uitableview, Caneditrowatindexpath Indexpath:nsindexpath), Bool { return True } //In the case of editable, by judging the type of editing, the implementation of specific logic, to achieve true editable. func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath indexpath:nsindexpath) { if Editingstyle = =. Insert { tableview.beginupdates () tableview.insertrowsatindexpaths ([Indexpath], Withrowanimation: uitableviewrowanimation.automatic) Datas.insert (("new title", "New Sub title"), AtIndex:indexPath.row) Print (datas) tableview.endupdates () } }
Move:
First enter the edit state @IBAction func edit (sender:uibarbuttonitem) { tableview.setediting (!tableview.editing, animated : true) } //Open edit mode func TableView (Tableview:uitableview, Caneditrowatindexpath Indexpath:nsindexpath)- > Bool { return True } //Whether a row can be moved. func TableView (Tableview:uitableview, Canmoverowatindexpath indexpath:nsindexpath), Bool { return True } Move the specific action of a row func TableView (Tableview:uitableview, Moverowatindexpath Sourceindexpath:nsindexpath, Toindexpath destinationindexpath:nsindexpath) {let fromIndex = Sourceindexpath.row let toindex = Destinationindexpath.row let movedata = Datas[fromindex] datas.removeatindex (fromIndex) Datas.insert (Movedata, Atindex:toindex) }
To set the index:
Set the text (index) of the sidebar, that is, the quick Find for contacts "abc...xyz" func Sectionindextitlesfortableview (Tableview:uitableview), [String]? { return ["A", "B"] } //Index Click event func tableView (Tableview:uitableview, Sectionforsectionindextitle Title:string, Atindex index:int), Int { //Click Index, jump to corresponding column group OK tableView. Scrolltorowatindexpath (< #T # # indexpath:nsindexpath# #NSIndexPath #>, atscrollposition: < #T # #UITableViewScrollPosition #>, animated: <# t# #Bool #>) }
Proxy method for Table viewcell Header footer display and stop display:
About to show cell Func TableView (Tableview:uitableview, Willdisplaycell Cell:uitableviewcell, Forrowatindexpath Indexpa Th:nsindexpath) {}//will show Footerview func TableView (Tableview:uitableview, Willdisplayfooterview view:uiv Iew, Forsection section:int) {}//will show Headerview func TableView (Tableview:uitableview, Willdispla Yheaderview View:uiview, Forsection section:int) {}//When a row Footerview stop showing, it is when Footerview disappears from the screen when it is called This method func TableView (Tableview:uitableview, Didenddisplayingfooterview View:uiview, forsection section:int) { }//When a line Headerview stops displaying, it is headerview to call this method when it disappears from the screen func TableView (Tableview:uitableview, Didenddisplay Ingheaderview View:uiview, Forsection section:int) {}//When a row cell stops showing, it is called when the cell disappears from the screen Fu NC tableView (Tableview:uitableview, Didenddisplayingcell Cell:uitableviewcell, Forrowatindexpath IndexPath: Nsindexpath) {}
the height of the cell header footer
Tell TableView the group head height func tableView (Tableview:uitableview, Heightforheaderinsection section:int), CGFloat { C3/>return 50.0 } //Tell TableView the group tail height func tableView (Tableview:uitableview, heightforfooterinsection Section:int), CGFloat { return 50.0 } //Returns the height of each row of cells func TableView (Tableview:uitableview, Heightforrowatindexpath Indexpath:nsindexpath), cgfloat { return 60.0 } override Func Viewdidload () { super.viewdidload () ///height can also be assigned by property Self.tableview.rowHeight = Self.tableview.sectionHeaderHeight = Self.tableview.sectionFooterHeight =
estimate the height of the cell header footer.
Note: This method needs to be used in conjunction with other methods, and this will later write the adaptive height to make the memo.
The estimated height of the cell is func tableView (Tableview:uitableview, Estimatedheightforrowatindexpath Indexpath:nsindexpath)- > cgfloat { return 100.0 } //Footer The estimated height of func tableView (Tableview:uitableview, Estimatedheightforfooterinsection section:int) --cgfloat {return 100.0 } //header estimated height func TableView (Tableview:uitableview, Estimatedheightforheaderinsection section:int), cgfloat { return 100.0 } The override Func Viewdidload () { super.viewdidload () //can also be assigned a value by property Self.tableview.estimatedRowHeight = self.tableview.estimatedSectionFooterHeight = Self.tableview.estimatedSectionFooterHeight = 60 }
Custom Group Header group footer
Back to Custom group footer view func tableView (Tableview:uitableview, Viewforfooterinsection section:int), UIView? {Let footer = UIView () Footer.backgroundcolor = Uicolor.redcolor () return footer } //Return to custom group header view func TableView (Tableview:uitableview, Viewforheaderinsection section:int), UIView? {Let header = UIView () Header.backgroundcolor = Uicolor.yellowcolor () return header }
Accessory Click events
Accessory type is the following two types to Cell.accessorytype = Uitableviewcellaccessorytype.detaildisclosurebutton Cell.accessorytype = Uitableviewcellaccessorytype.detailbutton //When the secondary button is clicked, the func TableView is triggered (TableView: UITableView, Accessorybuttontappedforrowwithindexpath indexpath:nsindexpath) { print (" Accessorybuttontappedforrowwithindexpath ") }
cell-click Callback
When the cell is clicked, the highlight Func TableView (Tableview:uitableview, Shouldhighlightrowatindexpath indexpath:nsindexpath) is displayed-& Gt Bool {return true}//cell has entered the highlighted state func TableView (Tableview:uitableview, Didhighlightrowatindexpath Indexpath:nsindexpath) {//Print ("Didhighlightrowatindexpath\ (Indexpath.row)")}//When the cell enters the highlighted state, call F UNC TableView (Tableview:uitableview, Didunhighlightrowatindexpath indexpath:nsindexpath) {//print ("Didunhighligh Trowatindexpath\ (Indexpath.row)}//A row cell is called when it is selected, return which line should be clicked Func TableView (Tableview:uitableview, will Selectrowatindexpath indexpath:nsindexpath), Nsindexpath? {Print ("Willselectrowatindexpath") return Indexpath}//A row cell is about to lose the selected state when calling Func TableView (ta Bleview:uitableview, Willdeselectrowatindexpath indexpath:nsindexpath), Nsindexpath? {print ("Willdeselectrowatindexpath \ (Indexpath.row)") return Indexpath}//When a row celL have been selected when calling Func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {print ("Didsel Ectrowatindexpath ")}//A row of cells has been lost when the selected state is called by Func TableView (Tableview:uitableview, Diddeselectrowatindexpath Indexpath:nsindexpath) {print ("Diddeselectrowatindexpath \ (Indexpath.row)")}
editing state styles and customizations
Returns the edit mode (Add/Remove) func TableView (Tableview:uitableview, Editingstyleforrowatindexpath in) of each row of cells when TableView enters the edit state Dexpath:nsindexpath), Uitableviewcelleditingstyle {return uitableviewcelleditingstyle.delete}// When the cell slides to the left, the caption of the delete button is displayed func TableView (Tableview:uitableview, Titlefordeleteconfirmationbuttonforrowatindexpath Indexpath:nsindexpath), String? {return ' delete '}//iOS8 can be used in a method that returns a Uitableviewrowaction array type, uitableviewrowaction can be customized, that is, the Edit button that appears after the left stroke is customized Func TableView (Tableview:uitableview, Editactionsforrowatindexpath indexpath:nsindexpath), [ Uitableviewrowaction]? {Let action = uitableviewrowaction (Style:UITableViewRowActionStyle.Default, title: "Custom Delete") {(uitableviewrowact Ion, Nsindexpath) in print ("Custom Delete callback")} Let Anotheraction = Uitableviewrowaction (style:u Itableviewrowactionstyle.normal, Title: "Custom") {(anotheraction, Indexpath) in print ("Custom Callback")} Anotheraction.backgroundcolor = Uicolor.bluecolor () Return [action, Anotheraction]} func Tablevi EW (Tableview:uitableview, Shouldindentwhileeditingrowatindexpath indexpath:nsindexpath), Bool {return True }//A row cell is about to enter edit mode, which is to slide to the left and the Edit button to display func TableView (Tableview:uitableview, Willbegineditingrowatindexpath IND Expath:nsindexpath) {print ("Willbegineditingrowatindexpath")}//A row cell has finished editing, just slide the cell to the right, close the edit button Func TableView (Tableview:uitableview, Didendeditingrowatindexpath indexpath:nsindexpath) {print ("Didendeditingr Owatindexpath ")}//Moves the line more than once in the process of calling this method, the return value represents the row returned after the move Operation Func TableView (Tableview:uitableview, TARGETINDEXPATHF Ormovefromrowatindexpath Sourceindexpath:nsindexpath, Toproposedindexpath Proposeddestinationindexpath: Nsindexpath), Nsindexpath {return Proposeddestinationindexpath}
cell Long Press the popup menu
Whether to allow long press pop -up menu func TableView (Tableview:uitableview, Shouldshowmenuforrowatindexpath Indexpath:nsindexpath)- > Bool { return True } //The method required for each row of cells. func TableView (Tableview:uitableview, canperformaction action:selector, Forrowatindexpath Indexpath: Nsindexpath, Withsender sender:anyobject?) -Bool { if action = = #selector (Nsobject.cut (_:)) { //do not show Cut function return false } return true< c12/>} //implementation of the specific function func tableView (Tableview:uitableview, performaction action:selector, Forrowatindexpath Indexpath:nsindexpath, Withsender sender:anyobject?) { }
Indentation of Cell
Sets the indentation of the cell, calling the func tableView (Tableview:uitableview, Indentationlevelforrowatindexpath when each row of cells is displayed) Indexpath:nsindexpath), Int { print ("Indentationlevelforrowatindexpath") return ten }
Table View Memo