【问题标题】:Xcode Swift show data from CloudKit in tableViewXcode Swift 在 tableView 中显示来自 CloudKit 的数据
【发布时间】:2016-02-25 12:50:24
【问题描述】:

希望有人可以帮助我。我试图在带有原型单元的 UITableview 中显示来自 CloudKit 的数据(privateData),但我的代码确实显示了存储在 CloudKit 中的数据。存储数据没问题。

我的代码是:

var arrayname = [CKRecord]()
var arraydate = [CKRecord]()

let privateDB = CKContainer.defaultContainer().privateCloudDatabase
let cloudContainer = (CKContainer.defaultContainer().addOperation)

func loadData() {

    arrayname = [CKRecord]()

    let publicData = CKContainer.defaultContainer().publicCloudDatabase
    let query = CKQuery(recordType: "Notes", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
    query.sortDescriptors = [NSSortDescriptor(key: "name", ascending: false)]
    publicData.performQuery(query, inZoneWithID: nil) { (results:[CKRecord]?, error:NSError?) -> Void in
        if let arrayName = results {
            self.arrayname = arrayName
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
            })
        }
    }   
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayname.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MainViewCell

    if arrayname.count == 0 {
        return cell
    }

    let name = arrayname[indexPath.row]

    if let nameContent = name["name"] as? String {
        cell.nameLabel?.text = nameContent
    }


    let date = arrayname[indexPath.row]

    if let dateContent = date["date_end"] as? String {
        cell.nameLabel?.text = dateContent
    }

    return cell
}

【问题讨论】:

  • step 出了什么问题?数据下载成功了吗?下载的数据是否成功转换为可用的 Swift 对象?表格是否显示了预期的行数和列数?有没有崩溃?
  • 当我在模拟器中运行应用程序时,我的 tableview 中没有显示任何数据。我保存数据没有问题,我可以在我的 CloudKit 仪表板中看到数据。 (我在模拟器设置中通过 iCloud 登录)。没有崩溃
  • 我已经检查了我所有的 CloudKit 设置,所以用户可以完全访问这些字段(读、写、查询)

标签: swift uitableview cloudkit


【解决方案1】:

让它工作:-) ......首先我必须为我的对象创建一个数组:

var arrayName: Array<CKRecord> = []

然后我查询数据:

 func loadData() {

    let container = CKContainer.defaultContainer()
    let privateDatabase = container.privateCloudDatabase
    let predicate = NSPredicate(value: true)

    let query = CKQuery(recordType: "Notes", predicate: predicate)

    privateDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
        if error != nil {
            print(error)
        }
        else {
            print(results)

            for result in results! {
                self.arrayName.append(result)
            }

            NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                self.tableView.reloadData()
                self.tableView.hidden = false
            })
        }
    }
}

然后我在我的表格视图中显示数据:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MainViewCell

    let noteRecord: CKRecord = arrayName[indexPath.row]

    cell.nameLabel?.text = noteRecord.valueForKey("name") as? String
    cell.dateLabel?.text = noteRecord.valueForKey("date_end") as? String

    return cell
}

【讨论】:

  • 我认为是我没有将云数据放入数组中:var arrayName: Array = []
【解决方案2】:

Flemming,CloudKit 的挑战有时是时机。在某些情况下,您几乎肯定会看到完成子句在数据返回之前返回。使用此功能 [我在 SO 上找到的,请原谅我不记得帖子] 来延迟表格填充的执行 [并确保它发生在主线程上]。

func delay(delay:Double, closure:()->()) {
dispatch_after(
    dispatch_time(
        DISPATCH_TIME_NOW,
        Int64(delay * Double(NSEC_PER_SEC))
    ),
    dispatch_get_main_queue(), closure)
}

所以你用 X 调用延迟;不知道我们在谈论多少数据或你链接的速度,所以我不能给你一个价值。但是,如果可行,请从 20 开始;减价。

delay(X) {
  self.arrayname = arrayName     
  self.tableView.reloadData()        
}

如果它有效,请给我点赞:)

【讨论】:

  • 无法让它工作。我要查询的数据只是文本而不是图像。当我使用 Parse 存储数据时,我不得不工作。
  • 如果你去仪表板,你会发现数据是;这里的错误发生了什么?你好像不检查?如果表中没有数据,放个else给你点什么?
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多