2015-07-10 61 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