这是我已经为iPhone制作的应用程序,并希望将其用于Apple Watch。
我有一个函数,它使用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 ?? ""
}
}
答案 0 :(得分: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()
}
}