2016-06-10 35 views
1

所以我試圖從一組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. 
} 
} 
} 
+0

請包括代碼和錯誤信息作爲實際文本,不僅僅作爲屏幕截圖。 – luk2302

+0

已更新。什麼時候屏幕截圖可以接受?代碼優先? –

+0

代碼屏幕截圖幾乎不是一個好主意,因爲任何想要回答的人都會想要做的第一件事是複製並將你的代碼粘貼到Xcode中。 –

回答

1

你確定這是你的代碼或者你可能意外刪除了一些行嗎?

因爲,您的表視圖函數實際上嵌入在您的viewDidLoad函數中......它們需要是對象級函數。

注意你的代碼的縮進級別時,你有Xcode的縮進的代碼爲你(在屏幕上單擊鼠標右鍵,選擇結構 - >重新縮進。

import UIKit 
import CloudKit 

class DiningTable: UITableViewController { 
    var categories: Array<CKRecord> = [] 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } // <--- you are missing this close brace here. 

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

    override func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int { 
     return self.categories.count 
    } 

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

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

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
1

整個班級的定義是嵌套的

override func viewDidLoad() { 

函數內。在打電話給super.viewDidLoad()後加上一個大括號。