2015-07-10 64 views
0

這是一款我已經爲iPhone製作的應用程序,並且希望將它用於Apple Watch。Swift:Watchkit Table not populating

我有一個函數,它使用SwiftyJSON來解析JSON並收集我用來填充我的表的各種信息。在willActiviate中,我循環遍歷所有行並將它們編入索引以獲取需要顯示在標籤上的信息。或者至少,這就是我想要做的。當我運行它時,會顯示正確數量的標籤,但標籤不顯示任何內容。我也創建了loadTableData()來手動重新加載tableData。

如果我在嘗試填充它時做了一些完全錯誤的事情,或者如果我在做其他事情時出錯,請向正確方向指出一點。

我的代碼:

 @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() 
    } 

} 

我的整個功能,我解析JSON如果需要的話:

,我用(如果需要)
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) 
        } 


       } 

      } 

     } 

我AppModel文件:

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 ?? "" 

    } 

} 

回答

1

我認爲你的問題是你試圖在willActivate中填充你的tableView,但你甚至不確定你是你已經有了數據(你的getEarthquakeInfo的完成處理程序可能無法到達)。

您應該在設置行數之後嘗試設置您的單元格。 順便說一句,你爲什麼設置兩倍的數量或行數?

嘗試是這樣的

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

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

      //Create cells 
      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 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() 


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

} 
+0

感謝您的幫助! (是的,當我設置兩次行數時,我實在不知道我在想什麼)。雖然現在我運行它,但是在'let time = info [self.earthTable.indexOfAccessibilityElement(currentRow)]。時出現一個錯誤,它顯示「致命錯誤:數組索引超出範圍」。我索引它錯了嗎? – videoperson

+1

難道你不能試着用'i'來代替'self.earthTable.indexOfAccessibilityElement(currentRow)',因爲你已經有索引了嗎? – Jaeger

+0

修好了!謝謝! – videoperson