繁体   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