2017-06-27 67 views
0

當我使用HKSampleQuery抽取我的HealthKit數據時,我創建了一個數組,然後填充tableview。但是,當我這樣做時,我的tableViewCell在血糖數量後還有許多其他字符。下面是該小區的截圖:從HealthKit中提取額外字符

enter image description here

繼承人在那裏我查詢數據。請任何幫助!

let endDate = NSDate() 
    let startDate = NSCalendar.current.date(byAdding: .day, value: number, to: endDate as Date) 
    let sampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose) 
    let mostRecentPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate as Date, options: []) 
    let query = HKSampleQuery(sampleType: sampleType!, predicate: mostRecentPredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, results, error) in 
     if let results = results as? [HKQuantitySample] { 
      self.bloodGlucose = results 
     } 
     DispatchQueue.main.async { 
      self.tableView.reloadData() 
     } 
    } 
    healthStore.execute(query) 

這裏就是我的設置...的tableview

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return bloodGlucose.count 
    } 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
       let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
       let sugar = bloodGlucose[indexPath.row] 
       currentCell.textLabel?.text = "\(sugar)" 
       currentCell.detailTextLabel?.text = dateFormatter.string(from: sugar.startDate) 
       return currentCell 
} 
+0

請包括您用於設置表格視圖單元格的代碼。 – Allan

+0

我編輯了這個問題。 @Allan – Johnd

回答

0

看來你是顯示的HKQuantitySample整個字符串表示。如果您只想顯示HKQuantitySample的數量,爲什麼不使用quantity屬性?

currentCell.textLabel?.text = "\(sugar.quantity)" 

順便說一句,你最好聲明你endDateDate(不NSDate):

let endDate = Date() 
let startDate = Calendar.current.date(byAdding: .day, value: number, to: endDate) 
let sampleType = HKSampleType.quantityType(forIdentifier: .bloodGlucose) 
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate) 

一般來說,非NS工種與斯威夫特更好。

+0

工作很好!謝謝! – Johnd

+1

請注意,以這種方式生成標籤文本永遠不會正確定位,從HKQuantity的'description'方法返回的字符串僅用於調試目的。如果你想要做的是顯示一個本地化的字符串表示,你需要使用類似'NSMeasurementFormatter'的東西。 – Allan