我的代码可以正常获取记录并将它们返回到我的UITableview,但问题是它返回Type中的所有记录。我通过NSUbiquitousKeyValueStore将用户保存到云中,从而吸引了用户。因此,每次用户登录时,我都希望他们能够在UITableView中只看到他们的信息(CKRecord中的字段)。
func fetch () {
if userLoad(){
let query = CKQuery(recordType: "User", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "username", ascending: false)]
query.sortDescriptors = [NSSortDescriptor(key: "type", ascending: false)]
query.sortDescriptors = [NSSortDescriptor(key: "hours", ascending: false)]
database.perform(query, inZoneWith: nil) { (results,error) in
if error != nil {
print("Error" + error.debugDescription)
} else {
for results2 in results!{
self.trucks.append(results2)
}
}
OperationQueue.main.addOperation({ () -> Void in
self.TableView.reloadData()
self.TableView.isHidden = false
})
print(results!)
}
} else {
print("no user currently available")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return trucks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
}
let cell = TableView.dequeueReusableCell(withIdentifier: "firstTableCell") as! FirstTableCell
if userLoad() {
let truck = trucks[indexPath.row]
cell.usernameLabel?.text = truck.value(forKey: "username") as? String
cell.typeLabel?.text = truck.value(forKey: "type") as? String
cell.hoursLabel?.text = truck.value(forKey: "hours") as? String
return cell
} else {
print("no user")
return cell
}
}