簡體   English   中英

Swift3,CloudKit和UITableView返回空白表

[英]Swift3, CloudKit and UITableView returns blank table

我在這里遇到了一個小小的障礙,我對Core Data非常滿意,並決定深入研究我的某些Apps的CloudKit,但是盡管上傳方面相當基礎,但是在填充簡單的表格視圖時遇到了麻煩。

CKRecord是Activity,我想顯示的字段是name。 打印函數print(actName)返回7次,顯示所有記錄均已計數,但表為空白且無錯誤。

我確信這很簡單,我看不到樹木的樹木,所以請為正確的方向感到高興。

干杯

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
    }

我似乎已經解決了問題,user3069232,我不認為存在/有很大的延遲問題,因為CloudKit Desktop即時更新了代碼。 Nirav我認為您說對了,可以歸結為不存儲然后重新加載。 我已經修改了原始代碼,因為我認為“活動”也引起了問題,下面的腳本運行良好,這要感謝方向正確的人。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM