2016-06-20 45 views
0

我謂詞/查詢/ performquery代碼工作正常,我的表格單元格設置了正確的TableCell控制器和可重複使用的標識顯示,我已經抽象從我CKRecordType的valueForKey的nameLabel和有未能加載/查看更新UI。CloudKit應用數據未在tableview中

import UIKit 
import CloudKit 

class table: UITableViewController { 
var categories: Array<CKRecord> = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

    func fetch() 
    { 
    categories = [] 
    let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase 
    let predicate = NSPredicate(value: true) 
    let query = CKQuery(recordType: "Dining", predicate: predicate) 
    publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in 
     if (error != nil) 
     { 
      print("Error" + (error?.localizedDescription)!) 
     } 
     else 
     { 
      for result in results! 
      { 
       self.categories.append(result) 
      } 
      NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in 
       self.tableView.reloadData() 
      }) 
     } 
     self.fetch()  
} 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return self.categories.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! tablecell 
    let restaurant: CKRecord = categories[indexPath.row] 
    cell.nameLabel.text = restaurant.valueForKey("Name") as? String 
    return cell 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

這裏,如果需要更多信息實際的項目:https://www.dropbox.com/sh/twt20r0sdauglhs/AAB6Y2CcxpT1hp0mBE_Znzgya?dl=0

回答

0

重新訪問一些較舊的代碼後,我能弄清楚,我需要我的類別變量設置爲

var categories: Array<CKRecord> = [] 

以及重新安排一些支架在我取功能。我需要放置取功能的調用函數(愚蠢的錯誤)之外,最後調用獲取後關閉viewDidLoad方法()。

func fetch() 
    {  
    let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase 
    let predicate = NSPredicate(value: true) 
    let query = CKQuery(recordType: "Dining", predicate: predicate) 
    publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in 
     if (error != nil) 
     { 
      print("Error" + (error?.localizedDescription)!) 
     } 
     else 
     { 
      for result in results! 
      { 
       self.categories.append(result) 
      } 

      NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in 
       self.tableView.reloadData() 
      }) 
     } 
     } 
} 
    fetch() 
}