希望有人可以帮助我。我试图在原型单元的UITableview中显示来自CloudKit的数据(privateData),但是我的代码做了bot,显示我存储在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
}
答案 0 :(得分:2)
让它工作:-) ......首先,我必须为我的对象制作一个数组:
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
}
答案 1 :(得分:0)
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()
}
如果有效,请给我一些荣誉:)