[英]Swift3, CloudKit and UITableView returns blank table
I am having a small mental block here, I am pretty comfortable with Core Data and decided to have a delve into CloudKit for some of my Apps, however whilst the upload side was fairly basic, I am having trouble populating a simple table view. 我在这里遇到了一个小小的障碍,我对Core Data非常满意,并决定深入研究我的某些Apps的CloudKit,但是尽管上传方面相当基础,但是在填充简单的表格视图时遇到了麻烦。
The CKRecord is Activity and the field I would like to display is name. CKRecord是Activity,我想显示的字段是name。 The print function print(actName) returns 7 times showing all the records have been counted but the table is blank and no errors.
打印函数print(actName)返回7次,显示所有记录均已计数,但表为空白且无错误。
I am sure this is something simple and i can't see the wood for the trees, so I am happy for a point in the right direction please. 我确信这很简单,我看不到树木的树木,所以请为正确的方向感到高兴。
Cheers 干杯
import UIKit
import CloudKit
class TableViewController: UITableViewController {
var activities = [Activity]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("counted records")
return activities.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let active = activities[indexPath.row]
if let actName = active.name {
cell.textLabel?.text = actName
print(actName)
}
return cell
}
I seem to have sorted it out, user3069232, I don't think there was/is a great latency problem as the updating code was instantaneous with CloudKit Desktop. 我似乎已经解决了问题,user3069232,我不认为存在/有很大的延迟问题,因为CloudKit Desktop即时更新了代码。 Nirav I think you were right it boiled down to not storing and then reloading.
Nirav我认为您说对了,可以归结为不存储然后重新加载。 I have modified my original code as I think 'Activity' was causing problems too, the script below works well, thanks for the point in the right direction guys.
我已经修改了原始代码,因为我认为“活动”也引起了问题,下面的脚本运行良好,这要感谢方向正确的人。
import UIKit
import CloudKit
class CoursesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var coursesTable: UITableView!
var coursesArray: Array<CKRecord> = []
var selectedCourseIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
coursesTable.delegate = self
coursesTable.dataSource = self
fetchCourses()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coursesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath)
let courseRecord: CKRecord = coursesArray[indexPath.row]
cell.textLabel?.text = courseRecord.value(forKey: "courseVenue") as? String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy"
let CSDate = dateFormatter.string(from: courseRecord.value(forKey: "courseDate") as! Date)
// let CSDetail = courseRecord.value(forKey: "courseDetail") as? String
cell.detailTextLabel?.text = CSDate
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCourseIndex = indexPath.row
performSegue(withIdentifier: "showMapDetail", sender: self)
}
func fetchCourses() {
let container = CKContainer.default()
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(format: "TRUEPREDICATE")
let query = CKQuery(recordType: "Courses", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "courseDate", ascending: true)]
publicDatabase.perform(query, inZoneWith: nil) { (results, error) -> Void in
if error != nil {
print("error fetch notes \(error)")
} else {
print("Success")
for result in results! {
self.coursesArray.append(result )
}
OperationQueue.main.addOperation({ () -> Void in
self.coursesTable.reloadData()
self.coursesTable.isHidden = false
})
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.