简体   繁体   中英

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.

The CKRecord is Activity and the field I would like to display is name. The print function print(actName) returns 7 times showing all the records have been counted but the table is blank and no errors.

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. Nirav I think you were right it boiled down to not storing and then reloading. 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.
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM