Swift: Watchkit Table not populating

This is an app that I already made for iPhone and want to make it available for the apple watch.

I have a function that uses SwiftyJSON to parse a JSON and collect various information that I use to populate my table. In willActiviate I loop through all of my rows and index them to get the information I need to display on the label. Or at least, that’s what I’m trying to do. When I run it, the correct amount of labels are shown, but the labels do not display anything. I’ve also created loadTableData() to manually reload the tableData.

If I’m doing something completely wrong when trying to populate it, or if I’m doing something else wrong, a little point in the right direction would be appreciated.

My code:

     @IBOutlet weak var earthTable: WKInterfaceTable!
    private func loadTableData() {
        getEarthquakeInfo { (info) in

            self.earthTable.setNumberOfRows(0, withRowType: "earthquake")
            self.earthTable.setNumberOfRows(info.count, withRowType: "earthquake")

        }

    }



    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)

            self.loadTableData()


    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
        for index in 0..<self.earthTable.numberOfRows {
            var currentRow = self.earthTable.rowControllerAtIndex(index) as earthquakeViewController


            let time = info[self.earthTable.indexOfAccessibilityElement(currentRow)].time
            let mag = info[self.earthTable.indexOfAccessibilityElement((currentRow))].mag
            let title = info[self.earthTable.indexOfAccessibilityElement(currentRow)].title
            currentRow.titleLabel.setText("(title)")
            currentRow.timeLabel.setText("(time)")
            currentRow.magLabel.setText("(mag)")




        }

            }
    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

}

My entire function where I parse the JSON if needed:

class InterfaceController: WKInterfaceController {

        var info = [AppModel]()

        func getEarthquakeInfo(completion: (results : [AppModel]) ->Void ){


            DataManager.getEarthquakeDataFromFileWithSuccess {
                (data) -> Void in

                let json = JSON(data: data)


                if var JsonArray =  json.array {
                    JsonArray.removeAtIndex(0)

                    for appDict in JsonArray {
                        // parsing
                        var ids: String? = appDict["id"].stringValue
                        var title: String? = appDict["title"].stringValue
                        var time: String = appDict["time"].stringValue
                        var lattitude: String? = appDict["lat"].stringValue
                        var longitude: String? = appDict["lng"].stringValue
                        var north: String? = appDict["north"].stringValue
                        var west: String? =  appDict["west"].stringValue
                        var mag: String? = appDict["mag"].stringValue
                        var depth: String? = appDict["depth"].stringValue
                        var timeStamp: String? = appDict["timestamp"].stringValue

                        // Splitting up title string into 2 parts
                        let newString = title!.stringByReplacingOccurrencesOfString("  ", withString: " - ", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        var title2strings = newString.componentsSeparatedByString(" - ")
                        var scale = title2strings[0]
                        var location = title2strings[1]

                        // replacing M in scale string with Richter Scale
                        let scaleString = scale.stringByReplacingOccurrencesOfString("ML", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString2 = scaleString.stringByReplacingOccurrencesOfString("mb", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString3 = scaleString2.stringByReplacingOccurrencesOfString("Mw", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString4 = scaleString3.stringByReplacingOccurrencesOfString("MD", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString5 = scaleString4.stringByReplacingOccurrencesOfString("M ", withString: "Magnitude ", options: NSStringCompareOptions.LiteralSearch, range: nil)

                        //Formatting the date
                        var date = NSDate(dateString: time).getDatePart()

                        // Collecting all the information
                        var information = AppModel(idEarth: ids, title: title, time: date, lat: lattitude, lng: longitude, north: north!, west: west, mag: mag, depth: depth, timeStamp: timeStamp, location: location, scale: scaleString5)
                        self.info.append(information)

                        //sorting array by highest magnitude
                        // self.info.sort({$0.mag > $1.mag})


                        // returning the completion handler
                        completion(results: self.info)
                    }


                }

            }

        }

My AppModel file that I used (if needed):

import Foundation
import WatchKit
class AppModel: NSObject, Printable {
    let idEarth: String
    let title: String
    let time: String
    let lat: String
    let lng: String
    let north: String
    let west: String
    let mag: String
    let depth: String
    let timeStamp: String
    let scale: String
    let location: String

    override var description: String {
        return "ID: (idEarth), TITLE: (title), TIME: (time), LAT: (lat), LNG: (lng), NORTH: (north), WEST: (west), MAG: (mag), DEPTH: (depth), TIMESTAMP: (timeStamp), LOCATION: (location), SCALE: (scale) n"
    }

    init(idEarth: String?, title: String?, time: String?, lat: String?, lng: String?, north: String, west: String?, mag: String?, depth: String?, timeStamp: String?, location: String?, scale: String?) {
        self.idEarth = idEarth ?? ""
        self.title = title ?? ""
        self.time = time ?? ""
        self.lat = lat ?? ""
        self.lng = lng ?? ""
        self.north = north ?? ""
        self.west = west ?? ""
        self.mag = mag ?? ""
        self.depth = depth ?? ""
        self.timeStamp = timeStamp ?? ""
        self.location = location ?? ""
        self.scale = scale ?? ""

    }

}


Source: json

Leave a Reply