2015-10-16 55 views
0

在我的應用程序中,我使用Parse SDK從數據庫中獲取藥品和金額列表,並將其通過iPhone傳遞到手錶。我手錶上的兩個獨立的sendMessages在WillActivate()來實現:使用解析在WatchOS中準備行

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

是,所述第二消息的回覆是覆蓋第一個表。你想達到什麼目的?在一張桌子上顯示藥物和金額? – joern

回答

0

要在你可以做以下的同桌顯示醫藥和金額:

  1. 創建屬性let medicines = [(String, String?)]()
  2. 當藥品到達填充藥品數組。所以,在此之後的藥物是這樣的[("Medicine1", nil), ("Medicine2", nil),...]
  3. 當到達量超過medicines迭代和批量添加到陣列,使其看起來像這樣之後:[("Medicine1", "Quantity1"), ("Medicine2", "Quantity2"),...]
  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] 
        } 
    } 
    
    +0

    感謝您的幫助,但我仍然遇到了問題。我的屬性數組看起來像這樣:「'drugs = [(String,String?)]()'」。從消息中我將得到兩個數組:「'medicineNames = [」Zyrtec「,」medicine1「,」medicine2「]'」和「'amountName = [」一天兩次「,」早上「,」之後「午餐「]''問題是誰將這些數組附加到屬性上? – kiuzio2

    +0

    請參閱我的更新答案,瞭解建議的方法。 – joern

    +0

    當我嘗試在行中使用'fun reloadTable()':if let row = self.table.rowControllerAtIndex(index)as? tableRowController' 顯示錯誤:'無法將類型值(UnsafePointer ,Int32) - > UnsafeMutablePointer '轉換爲期望的參數類型'Int'' – kiuzio2