2016-04-23 66 views
-1

我創建從火力地堡檢索數據的應用程序。在我的'MealViewController'中,我有一個TableView,它具有視圖控制器,因爲它是委託和數據源。我得到的問題「Type'MealViewController」不符合協議'UITableViewDataSource',因爲它需要:numberOfRowsInSection:和:cellForRowAtIndexPath:。但是,當我將兩者相加時,會出現另一個問題 - 「定義與以前的值衝突」。我查看了與此相關的所有堆棧溢出問題,並沒有運氣。這裏是我的視圖控制器:的cellForRowAtIndexPath和numberOfRowsInSection相互矛盾的的tableView

class MealViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var bgImage: UIImageView? 
    var image : UIImage = UIImage(named: "pizza")! 
    @IBOutlet weak var blurEffect: UIVisualEffectView! 
    @IBOutlet weak var mealTableView: UITableView! 
    var items = [MealItem]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     bgImage = UIImageView(image: image) 
     bgImage?.contentMode = .ScaleAspectFill 
     bgImage!.frame = view.layer.bounds 

     self.view.addSubview(bgImage!) 
     //self.bgImage?.addSubview(blurEffect) 
     //bgImage!.bringSubviewToFront(blurEffect) 
     view.bringSubviewToFront(blurEffect) 

     mealTableView.layer.cornerRadius = 5.0 
     mealTableView.layer.borderColor = UIColor.whiteColor().CGColor 
     mealTableView.layer.borderWidth = 0.5 


     let ref = Firebase(url: "https://order-template.firebaseio.com/grocery-items") 

     mealTableView.delegate = self 
     mealTableView.dataSource = self 


     // MARK: UIViewController Lifecycle 

     func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

      return 1 
     } 

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      print(items.count) 
      return items.count 

     } 

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> MealsCellTableViewCell { //issue occurs here 

      let groceryItem = items[indexPath.row] 

      if let cell = mealTableView.dequeueReusableCellWithIdentifier("ItemCell") as? MealsCellTableViewCell { 


       cell.configureCell(groceryItem) 

       // Determine whether the cell is checked 

       self.mealTableView.reloadData() 

       return cell 

      } 
     } 

     func viewDidAppear(animated: Bool) { 
      super.viewDidAppear(animated) 

      // [1] Call the queryOrderedByChild function to return a reference that queries by the "completed" property 
      ref.observeEventType(.Value, withBlock: { snapshot in 

       var newItems = [MealItem]() 

       for item in snapshot.children { 

        let mealItem = MealItem(snapshot: item as! FDataSnapshot) 
        newItems.append(mealItem) 
       } 

       self.items = newItems 
       self.mealTableView.reloadData() 
      }) 

     } 



     func viewDidDisappear(animated: Bool) { 
      super.viewDidDisappear(animated) 

     } 

     func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 



     } 



    } 

    override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 

     bgImage = UIImageView(image: image) 
     bgImage?.contentMode = .ScaleAspectFill 

     bgImage!.frame = view.layer.bounds 
     self.view.addSubview(bgImage!) 
     view.bringSubviewToFront(blurEffect) 

    } 


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


     // MARK: UITableView Delegate methods 






} 
+0

請在顯示該錯誤的行上顯示。也許這與表格視圖無關。 –

+0

第一個問題是在類的語句,第二個我編輯和標記,這是在的cellForRowAtIndexPath –

+0

哦,只注意到你的數據源的方法實際上是嵌套:)因此,你的'cellForRowAtIndexPath'僅僅是一個局部的功能,這是看不見的'viewDidLoad'之外。您應該將所有嵌套函數移出'viewDidLoad'。 –

回答

2

的的cellForRowAtIndexPath應該是這樣的:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cellIdentifier = "ItemCell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MealsCellTableViewCell 

    let groceryItem = self.items[indexPath.row] 

    cell.configureCell(groceryItem) 

    return cell 
} 

注意,返回小區是MealsCellTableViewCell是的UITableViewCell的子類,所以它符合該類。

不要更改函數的定義,這將使它不符合什麼樣的委託協議規定。

這裏的蘋果文檔的鏈接的具體實施定製的tableView細胞以供參考。

Create a Table View

1

的問題是,您的視圖控制器的一致性UITableViewDatasource cellForRowAtIndexPath方法是不正確的。你應該重構你的cellForRowAtIndexPath方法的實現,像這樣:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let groceryItem = items[indexPath.row] 

    guard let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell") as? MealsCellTableViewCell else { 
     fatalError("No cell with identifier: ItemCell") 
    } 

    cell.configureCell(groceryItem) 

    return cell 
} 

您還需要移動數據源的方法進行viewDidLoad方法。

+0

如果mealTableView委託移出viewDidLoad()的問題比「預期聲明出現」的問題。在改變實現之後,仍然存在不符合協議dataSource的問題,並且「無法將類型'MealsCellTableViewCell'的返回表達式轉換爲返回類型'UITableView'」 –

0

您返回MealsCellTableViewCell而不是UITableViewCellcellForRowAtIndexPath方法,這就是原因。

+0

這兩個'定義與以前的值衝突的問題'並且不符合類仍然存在 –

+0

@JohnHarding只要MealsCellTableViewCell是一個UITableViewCell,那麼從方法中返回它是很好的,因爲它是一種UITableViewCell類型 - 這是很常見的做法。但是,你的函數定義應該返回UITableViewCell(所以它符合)而不是MealsCellTableViewCell – Jay