【问题标题】:CloudKit app data not displaying in tableviewCloudKit 应用程序数据未显示在表格视图中
【发布时间】:2016-06-20 11:50:00
【问题描述】:

我的谓词/查询/执行查询代码正常,我的表格单元设置了正确的表格单元控制器和可重用标识符,我已经从我的 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

【问题讨论】:

    标签: swift uitableview cloudkit


    【解决方案1】:

    重新访问一些旧代码后,我发现我需要将类别变量设置为

    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()   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 2019-07-30
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多