我的谓词/查询/执行查询代码处于正常工作状态,我的表格单元格设置了正确的表格单元格控制器和可重复使用的标识符,我从CKRecordType的valueForKey中抽象出了nameLabel,并且无法加载/查看更新的用户界面。
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 :(得分:0)
重新访问一些旧代码后,我能够发现我需要将我的类别变量设置为
var categories: Array<CKRecord> = []
以及重新排列我的fetch函数中的一些括号。我需要在函数外部调用fetch函数(愚蠢的错误),最后在调用fetch()后关闭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()
}