所以我试图用一组 cloudkit 下载的项目制作一个 tableview。但是,我遇到了这个简单的“定义与先前值冲突”的小错误。 cellForRowAtIndexPath 函数会发生此错误 - 据推测它与 numberOfRowsInSection 函数冲突。我试图移动/删除后一个函数的括号,并在末尾放置一个问号/可选...“-> UITableViewCell?”无济于事。错误可能来自哪里?
此外,就目前而言,xcode 删除了每个 tableview 函数的覆盖部分。为什么有时会保留,何时删除?
import UIKit
import CloudKit
class DiningTable: UITableViewController {
var categories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
func getRecords()
{
categories = []
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["Name", "Address", "Picture"]
queryOperation.qualityOfService = .UserInteractive
queryOperation.recordFetchedBlock = { (record:CKRecord) -> Void in
let categoryRecord = record
self.categories.append(categoryRecord)
}
queryOperation.queryCompletionBlock = { (cursor:CKQueryCursor?, error: NSError?) -> Void in
if (error != nil) {
print("Failed to get data from iCloud - \(error!.localizedDescription)")
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
publicDatabase.addOperation(queryOperation
}
func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
return self.categories.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! DiningTableCell
let restaurant: CKRecord = categories[indexPath.row]
cell.RestaurantName?.text = restaurant.valueForKey("Name") as? String
let img = restaurant.objectForKey("Picture") as! CKAsset
cell.RestaurantPhoto.image = UIImage(contentsOfFile: img.fileURL.path!)
return cell
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue1" {
if let destViewController = segue.destinationViewController as? RestaurantTable {
let indexPath = self.tableView.indexPathForSelectedRow!
destViewController.indexpath1 = indexPath
}
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
}
最佳答案
你的整个类定义嵌套在你的
override func viewDidLoad() {
函数。在调用 super.viewDidLoad() 之后放置一个右括号
关于swift - 定义与以前的值冲突 - 另外,何时删除覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758344/