gpt4 book ai didi

ios - 在 WatchOS 中使用 parse 准备行

转载 作者:行者123 更新时间:2023-11-30 13:59:24 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 Parse SDK 从数据库中获取药物列表和数量,并将其通过 iPhone 传递到 watch 。我在 watch 上实现了 WillActivate() 中的两个单独的 sendMessage:

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 。是因为两条不同的消息吗?

最佳答案

要在同一个表中显示药物和数量,您可以执行以下操作:

  1. 创建一个属性 let drugs = [(String, String?)]()
  2. 当药物到达时,用药物填充该阵列。这样药物看起来就像这样 [("Medicine1", nil), ("Medicine2", nil),...]
  3. 当数量到达时,迭代 medicines 并将数量添加到数组中,使其看起来像这样:[("Medicine1", "Quantity1"), ("医学2”,“数量2”),...]
  4. 使用 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]
    }
    }

    关于ios - 在 WatchOS 中使用 parse 准备行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174065/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com