tableView

时间:2016-04-23 21:55:02

标签: ios swift uitableview firebase

我正在创建一个从Firebase检索数据的应用。在我的MealViewController'我有一个TableView,它有视图控制器作为它的委托和数据源。我遇到了问题"键入' MealViewController"不符合协议' UITableViewDataSource'因为它需要:numberOfRowsInSection:和:cellForRowAtIndexPath :.但是,当我添加两者时,会出现另一个问题 - '定义与之前的值冲突'。我已经查看了与此相关的所有Stack Overflow问题,但没有运气。这是我的视图控制器:

    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






}

3 个答案:

答案 0 :(得分: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的子类,因此它符合该类。

请勿更改函数定义,因为这将使其不符合委托协议指定的内容。

这是Apple文档的链接,用于自定义tableView单元的特定实现以供参考。

Create a Table View

答案 1 :(得分: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方法。

答案 2 :(得分:0)

您在MealsCellTableViewCell方法中返回UITableViewCell而不是cellForRowAtIndexPath,这就是原因。