Search code examples
iosxcodeswiftuitableviewwatchkit

Set up WatchKit Table


I'm trying to load my data into a WatchKit table. Basically, set the text of the match label in each table group cell with the array of matchs I have.

I've got the data, and everything set up, but actually loading it into the table is where I'm stuck.

InterfaceController.swift:

    var receivedData = Array<Dictionary<String, String>>()

    var eventsListSO = Array<Event>()

    @IBOutlet var rowTable: WKInterfaceTable!

    func doTable() {

        eventsListSO = Event.eventsListFromValues(receivedData)
        rowTable.setNumberOfRows(eventsListSO.count, withRowType: "rows")

        for var i = 0; i < self.rowTable.numberOfRows; i++ {
            let row = rowTable.rowControllerAtIndex(i) as? TableRowController
            for eventm in eventsListSO {
                row!.mLabel.setText(eventm.eventMatch)
                NSLog("SetupTableM: %@", eventm.eventMatch)
            }
        }

    }

I was trying to do it in doTable because that seemed like best place to do this, and I think doTable is set up right, but I'm not sure? Not sure if I need to make the array an optional type or what.

Here is the referencing code if needed:

RowController.swift:

class TableRowController {

    @IBOutlet var mLabel: WKInterfaceLabel!
    @IBOutlet var cGroup: WKInterfaceGroup!

}

Event.swift:

class Event {

    var eventTColor:String
    var eventMatch:String

    init(dataDictionary:Dictionary<String,String>) {
        eventTColor = dataDictionary["TColor"]!
        eventMatch = dataDictionary["Match"]!
    }

    class func newEvent(dataDictionary:Dictionary<String,String>) -> Event {
        return Event(dataDictionary: dataDictionary)
    }

    class func eventsListFromValues(values: Array<Dictionary<String, String>>) -> Array<Event> {
        var array = Array<Event>()

        for eventValues in values {
            let event = Event(dataDictionary: eventValues)
            array.append(event)
        }

        return array
    }

}

So I'm not sure if:

- doTable is set up right (can't be because eventsListSO.count is null)


Solution

  • The way you work with tables in WatchKit is a lot different than UIKit.

    After you call setNumberOfRows you need to iterate over each row and get the RowController.

    for var i = 0; i < self.rowTable.numberOfRows; i++ {
        var row = self.rowTable.rowControllerAtIndex(i)
        //setup row here
    }