Considering Table Input

If you look at the documentation for WKInterfaceTable, you may notice that there is no delegate property nor (seemingly) any way to handle when the user taps your table. Is this some massive oversight of WatchKit? Do you need to search for a cryptic informal protocol in Objective-C? Turns out the answer is no for both questions. When the user interacts with a WKInterfaceTable, those interactions are automatically forwarded to the table’s containing interface controller, much like the responder chain automatically forwards touch events on iOS. Specifically, you want to look at WKInterfaceController’s table(_:didSelectRowAtIndex:) method, which watchOS will call whenever the user taps a row.

In your table, you’ll probably want to show the details for a run when the user taps a row. We’ll cover displaying another interface controller later, so for now let’s just display a message so you know it works. Implement table(_:didSelectRowAtIndex:) like so:

 override​ ​func​ table(table: ​WKInterfaceTable​,
  didSelectRowAtIndex rowIndex: ​Int​) {
 NSLog​(​"User tapped on row ​​(​rowIndex​)​​"​)
 }

Build, run and then tap a row. You’ll see the log message appear in the console in Xcode. In a real app, you’d want to do something with this action, but this is enough to see how simple it is to interact with table rows in your app.

Some apps will require more advanced layouts than simple rows. If you add a button, switch, slider, or other interactive control to your table row, you’ll need to use action methods on those objects. When the user taps an interface object that can accept a tap, your table(_:didSelectRowAtIndex:) method is not called. This affords you some extra ways to interact with your app, but it can be more complicated and a bit harder to use. If you add interface objects that receive taps, such as buttons or switches, to the row, those objects will receive taps when the user taps them. If the user taps outside those interface objects but still on your row, then watchOS will call your table(_:didSelectRowAtIndex:) method. Since you’re dealing with such small screens here, it’s best to keep the table row to a limited set of controls—preferably two at most, including tapping the row itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.237.176