• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Tutorial – Swift Watchkit Table

Home Apple Watch Tutorial – Swift Watchkit Table

Tutorial – Swift Watchkit Table

Mar 6, 2015 | Posted by Andrew | Apple Watch, swift, tutorial, watchkit |

In this post we will look at implementing a table in XCode.

If you have ever done a tableview in a iPhone app forget everything you know, as it is implemented differently in a watch kit app. It’s not to difficult to implement through!

First of create a new blank XCode project  and add a Watchkit extension in. Once done go to the watch kit interface and drag a table from the bottom right onto the main watch kit interface controller.

Screen Shot 2015-03-05 at 6.02.13 pm

 

 

Now we need to connect the table up to our InterfaceController.swift class. To do this click on the assistant editor assisteditor to split up the view. Now make sure the table is selected under interface controller.

Screen Shot 2015-03-05 at 6.10.02 pmw

Drag it into your InterfaceController.swift code and drop it just under the Class InterfaceController, now you will see a popup. Name it tableview.

. Screen Shot 2015-03-05 at 6.08.35 pm

 

Once this has been made, add the following line under:

var tableData = [“I”, “Love”, “Apple”, “Watch”, “:)”, “:)”]

This will be each item in our table, in our table we will just have tables with text.

Now we need to setup a custom class for our TableRow. To do this go File–>New–>File. Make a new NSObject type named TableRowObject.

Screen Shot 2015-03-06 at 6.33.07 pm

 

Once created go back to the Interface.storyboard for the Watchkit app, we need to set our TableRow to be of the TableRowObject swift class we just created.

Select the Table Row Object on the interface controller, then in the top right set the class to “TableRowObject” under the Identity Inspector.

Screen Shot 2015-03-06 at 6.35.44 pmScreen Shot 2015-03-06 at 6.37.27 pm

 

Once done select the attributes tab (On the right of identity inspector, and set the Identifier to “Cell”

Screen Shot 2015-03-06 at 6.38.41 pm

Now we are going to add a label to our table and connect it to our TableRowObject.swift class so we can set it’s value!

Still on the interface inspector, drag a label onto your table row. Once done make sure Cell is selected on the Interface Controller. Click the assistant editorassisteditor

Screen Shot 2015-03-06 at 6.40.38 pm

 

Make sure TableRowObject is selected in the right window of the split view.Screen Shot 2015-03-06 at 6.44.02 pm

Select the label and hold control to drag it into your class. Name it lblTblRowItem

Screen Shot 2015-03-06 at 6.47.30 pm

Also up the top of the class add import WatchKit so it can access the label correctly (As it is a special Watchkit label)

Your class should look like the following:

import UIKit
import WatchKit // Need this to access WK Class

class TableRowObject: NSObject {
    @IBOutlet var lblTblRowItem: WKInterfaceLabel!
}

Now time to add in our code to load our items into the table, then we can test it out!

Go to your InterfaceController.swift class and add the following function:

// Load table into the data
func loadTableData() {
    // Set total row count. Remember our identifier was Cell
    tableView.setNumberOfRows(tableData.count, withRowType: "Cell")
    
    var i = 0 // Used to count each item
    for item in tableData { // Loop over each item in tableData
        let row = tableView.rowControllerAtIndex(i) as TableRowObject // Get a single row object for the current item
        row.lblTblRowItem.setText(item) // Set the row text to the corresponding item
        i++ // Move onto the next item
    }
    
}

This function will add all the items from tableData (I, Love, Apple, Watch, :), :)) into our table. Now in  awakeWithContext add loadTableData() to load everything into your table!

override func awakeWithContext(context: AnyObject?) {
   super.awakeWithContext(context)
        
   // Configure interface objects here.
   loadTableData()
}

Run the app as a Watchkit app and you will see your table in action!, Source code can be downloaded below.

Screen Shot 2015-03-06 at 6.59.14 pm

DownloadSourceCode

Tags: tableWKInterfaceTable
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift – Filter Bar Tutorial

Mar 9, 2017

In our previous tutorial we implemented a search bar for[...]

Apple Watch, WatchKit First App Setup – Hello World

Mar 3, 2015

Setting up a watch kit app for the Apple Watch[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next