在我的应用程序中,我使用Parse SDK从数据库中获取药物和金额列表,并将其通过iPhone传递给手表。我在手表上实现了WillActivate()中的两个单独的sendMessages:
let iNeedMedicine = ["Value": "Query"]
session.sendMessage(iNeedMedicine, replyHandler: { (content:[String : AnyObject]) -> Void in
if let medicines = content["medicines"] as? [String] {
print(medicines)
self.table.setNumberOfRows(medicines.count, withRowType: "tableRowController")
for (index, medicine) in medicines.enumerate() {
let row = self.table.rowControllerAtIndex(index) as? tableRowController
if let row1 = row {
row1.medicineLabel.setText(medicine)
}
}
}
}, errorHandler: { (error ) -> Void in
print("We got an error from our watch device : " + error.domain)
})
第二
let iNeedAmount = ["Value" : "Amount"]
session.sendMessage(iNeedAmount, replyHandler: { (content:[String : AnyObject]) -> Void in
if let quantity = content["quantity"] as? [String] {
print(quantity)
self.table.setNumberOfRows(quantity.count, withRowType: "tableRowController")
for (index, quant) in quantity.enumerate() {
let row = self.table.rowControllerAtIndex(index) as? tableRowController
row!.amountLabel.setText(quant)
}
}
}, errorHandler: { (error ) -> Void in
print("We got an error from our watch device : " + error.domain)
})
我得到的是:Problem。是因为两条不同的消息吗?
答案 0 :(得分:0)
要在同一张表格中显示药物和金额,您可以执行以下操作:
let medicines = [(String, String?)]()
[("Medicine1", nil), ("Medicine2", nil),...]
medicines
并将数量添加到数组中,以便在此之后看起来像这样:[("Medicine1", "Quantity1"), ("Medicine2", "Quantity2"),...]
medicines
数组填充表格。创建一个重新加载表的方法:像这样:
func reloadTable() {
self.table.setNumberOfRows(medicines.count, withRowType: "tableRowController")
var rowIndex = 0
for item in medicines {
if let row = self.table.rowControllerAtIndex(rowIndex) as? tableRowController {
row.medicineLabel.setText(item.0)
if let quantity = item.1 {
row.quantityLabel.setText(quantity)
}
rowIndex++
}
}
}
reloadTable()
。 这只是解释这个想法的一个原始例子。您必须小心保持药物和数量同步。特别是当用户向下滚动时加载更多数据。
要将消息中的数据填充到数组中,您可以定义两个函数:
func addMedicines(medicineNames: [String]) {
for name in medicineNames {
medicines.append((name, nil))
}
}
func addQuantities(quantities: [String]) {
guard medicines.count == quantities.count else { return }
for i in 0..<medicines.count {
medicines[i].1 = quantities[i]
}
}