简体   繁体   English

从CloudKit提取多种记录类型

[英]Fetch multiple Record Types from CloudKit

I have a segmented control with four different segments and would like to fetch multiple record types at once. 我有一个具有四个不同段的分段控件,想一次获取多个记录类型。 Have only managed to fetch one at a time. 一次只能获取一个。 Would like to fetch all the 4 record types, use segmented control to display them in their corresponding tableviews. 要获取所有4种记录类型,请使用分段控件将它们显示在相应的表视图中。 Is there anyway way. 反正有。 I'm fairly new to Swift and iOS. 我是Swift和iOS的新手。

@IBOutlet weak var segmentControl: UISegmentedControl!

let recordType = "WebBooks"

var web = [CKRecord]()
var mobile = [CKRecord]()
var windows = [CKRecord]()
var databases = [CKRecord]()

func fetchBooksFromCloud() {
        let cloudContainer = CKContainer.default()
        let publicDatabase = cloudContainer.publicCloudDatabase
        let predicate = NSPredicate(value: true)
        let query = CKQuery(recordType: recordType, predicate: predicate)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var returnValue = 0

    switch segmentControl.selectedSegmentIndex {
    case 0:
        returnValue = web.count  
    default:
        break
    }
    return returnValue
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: 
    "CategoriesCell", for: indexPath) as! HomeTableViewCell

    switch segmentControl.selectedSegmentIndex {
    case 0:

        let webBooks = web[indexPath.row]

        cell.bookName.text = web.object(forKey: "name") as? String
        cell.authorName.text = web.object(forKey: "author") as? String

        if let image = web.object(forKey: "image") {
            let imageAsset = image as! CKAsset

            if let imageData = try? Data.init(contentsOf: imageAsset.fileURL) {
                cell.bookImageName.image = UIImage(data: imageData)
            }
        }
    default:
        break
    }
    return cell
}

for more reference : iCloud in Swift 有关更多参考: Swift中的iCloud

func getResults(){

        let container = CKContainer.default()
        let privateDatabase = container.privateCloudDatabase
        let predicate = NSPredicate(value: true)
        let query = CKQuery(recordType: "UserDetails", predicate: predicate)

        privateDatabase.perform(query, inZoneWith: nil) { (results, error) -> Void in
            if error != nil {
                print(error?.localizedDescription)

                MBProgressHUD.hide(for: self.view, animated: true)
            }
            else {
                print(results)

                for result in results! {
                    self.arrayDetails.append(result)
                }

                OperationQueue.main.addOperation({ () -> Void in
                    self.tableView.reloadData()
                    self.tableView.isHidden = false
                    MBProgressHUD.hide(for: self.view, animated: true)
                })
            }
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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