我有一个带有四个不同段的分段控件,希望一次获取多个记录类型。一次只能取一个。要获取所有4个记录类型,请使用分段控件将它们显示在相应的表视图中。有办法吗。我对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
}

最佳答案

更多参考:iCloud in Swift

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)
                })
            }
        }

}

10-08 01:02