Introduction to AppleWatch Development Four--table view applicationOne, watch on the table
There is a big difference between tableview in watchOS and TableView in iOS, and before development, we should first understand the limitations and features of table in watchOS. Here are some of the special things I've summed up in table watchOS:
1, table only the concept of the row, there is no concept of partitioning, there is no concept of the tail view.
2, you can create multiple table, to achieve the effect of partitioning.
3, because watch on the layout through the GRUOP to adapt, so there is no high-line setting.
4, table does not have a proxy, all rows of data are in a static configuration method, will be described later.
5. Clicking on a row trigger in table is done by rewriting the method in interface.
Second, create a table
Drag your table into the storyboard as follows:
Pull two labels on table:
Each table contains a tablerowcontroller, and actually the controls on our table are managed by this tablerowcontroller, so if we need to control the content on the TableRow in the code, We need to create a file as a table Tablerowcontroller:
Modify the Tablerowcontroller class in storyboard to the class we created and specify a identifier:
We then associate two labels to the Tablerowcontroller:
Import Watchkitclass tablerowcontroller:nsobject {@IBOutlet var numberlabel:wkinterfacelabel! @IBOutlet var titlelabel:wkinterfacelabel!}
To associate a table to the Interfacecontroller:
Class Interfacecontrollermain:wkinterfacecontroller {@IBOutlet var table:wkinterfacetable!}
below, We started to configure table in interface, first we can look at the methods and properties in Wkinterfacetable:
public class wkinterfacetable : wkinterfaceobject { //sets the type of row, The type of row in the array, the number of elements in the array, the number of rows /* by this method, we can create a table with a different row style, a row type is actually the tablerowcontroller we just used, we can customize */ public func setrowtypes (rowtypes: [string]) //set line number and type Table public func setnumberofrows for creating a single row type (numberofrows: int, withrowtype rowtype: string) // repeating row name // This get method gets the number of rows that we use to iterate through the rows in the table for content settings public var numberofrows: int { get } //This method will return a row, we can get to the content settings after public func rowcontrolleratindex (index: int) -> anyobject? //insert a row public func&nBsp;insertrowsatindexes (rows: nsindexset, withrowtype rowtype: string) //Delete a row public func removerowsatindexes (Rows: nsindexset) //slide to a line public func scrolltorowatindex (INDEX: INT)}
Knowing the above method, we can see that the table configuration of watchOS is very simple and easy to use, for example, we configure the following:
@IBOutlet var Table: WKInterfaceTable! override func Awakewithcontext (context: anyobject?) { super.awakewithcontext (context) let dic:dictionary<string,string> = ["China Construction Bank": "¥1000", "Agricultural Bank of China": " ¥5000 "," Bank of China ":" 20000 "," China Merchants Bank ":" ¥401 "," Chinese Postal Savings ":" 1100 "] //set number of rows and types table.setnumberofrows (dic.count, withrowtype: " Tablerowcontroller ") //Traversal for settings let titlearray:array<string> = array (Dic.keys) for var i=0 ; i < dic.count ; i++ { let row:tablerowcontrolleR = table.rowcontrolleratindex (i) as! TableRowController row.titlelabel.settext (Titlearray[i]) row.numberlabel.settext (Dic[titlearray[i]) row.numberlabel.settextcolor (UIColor.grayColor ()) } // Configure interface objects here. }
Such a display of the bank card balance of the interface we created, the effect is as follows:
Third, about the table click event
As we mentioned above, table does not have the so-called proxy method, click on the row, we are also in two ways to carry out the logical jump, one is in the storyboard, we jump through the pull line, when the need to pass value, we need to implement the following methods in interface:
public func contextforseguewithidentifier (segueidentifier:string, intable table:wkinterfacetable, Rowindex:int)- > Anyobject?
In another way, we can override the following methods in implementing Interfacecontroller to handle table click events:
Public func table (table:wkinterfacetable, Didselectrowatindex rowindex:int)
Either way, we can use the Parameters table and rowindex to confirm that the click is exactly the table and which row, to pass the value and to handle our logic.
Introduction to AppleWatch Development Four--table view application