简体   繁体   English

tableView中的cellForRowAtIndexPath和numberOfRowsInSection冲突

[英]cellForRowAtIndexPath and numberOfRowsInSection conflicting in tableView

I am creating an app that is retrieving data from Firebase. 我正在创建一个从Firebase检索数据的应用程序。 In my 'MealViewController' I have a TableView that has the view controller as it's delegate and data source. 在我的“ MealViewController”中,我有一个TableView,它具有视图控制器作为其委托和数据源。 I am getting the issue "Type 'MealViewController" does not conform to protocol 'UITableViewDataSource' because it requires both :numberOfRowsInSection: and :cellForRowAtIndexPath: . 我遇到问题“类型'MealViewController'不符合协议'UITableViewDataSource'的问题,因为它同时需要:numberOfRowsInSection:和:cellForRowAtIndexPath:。 However, when I add both, another issue appears - 'Definition conflict with previous value'. 但是,当我同时添加两者时,会出现另一个问题-“定义与先前的值冲突”。 I've looked through all the Stack Overflow issues related to this, and no luck has been had. 我已经仔细研究了与此相关的所有堆栈溢出问题,但还没有碰运气。 Here's my View Controller: 这是我的View Controller:

    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






}

The cellForRowAtIndexPath should look like this: 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
}

Note that the returned cell is a MealsCellTableViewCell which is a subclass of UITableViewCell so it conforms to that class. 请注意,返回的单元格是MealsCellTableViewCell,它是UITableViewCell的子类,因此符合该类。

Don't change the function definition as that will make it not conform to what the delegate protocol specifies. 请勿更改函数定义,因为这将使其不符合委托协议指定的内容。

Here's a link to the Apple documentation for the specific implementation of custom tableView cells for reference. 这是指向Apple文档的链接,用于自定义tableView单元格的特定实现,以供参考。

Create a Table View 创建表视图

The problem is that your view controller's conformance to UITableViewDatasource cellForRowAtIndexPath method is not right. 问题是您的视图控制器与UITableViewDatasource cellForRowAtIndexPath方法的一致性不正确。 You should refactor your implementation of cellForRowAtIndexPath method like so: 您应该像这样重构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
}

You also need to move the datasource methods out of viewDidLoad method. 您还需要将数据源方法移出viewDidLoad方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未调用 cellForRowAtIndexPath 但调用了 numberOfRowsInSection - cellForRowAtIndexPath not called but numberOfRowsInSection called numberOfRowsInSection和cellForRowAtIndexPath的2个问题 - 2 problems with numberOfRowsInSection and cellForRowAtIndexPath cellForRowAtIndexPath永远不会被调用,但是numberOfRowsInSection是 - cellForRowAtIndexPath never gets called, but numberOfRowsInSection is 使用tableView numberOfRowsInSection时遇到问题 - Trouble using tableView numberOfRowsInSection 在numberOfRowsInSection中调用TableView BeginUpdates - Calling TableView BeginUpdates in numberOfRowsInSection [tableView numberOfRowsInSection:0始终返回0 - [tableView numberOfRowsInSection:0 always returns 0 在reloadData之后没有调用cellForRowAtIndexPath但是numberOfRowsInSection没有调用 - cellForRowAtIndexPath not called after reloadData but numberOfRowsInSection does UITableViewController不会调用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection设置正确 - UITableViewController will not call cellForRowAtIndexPath BUT numberOfSectionsInTableView and numberOfRowsInSection are set properly numberOfRowsInSection多次调用,但从未调用cellForRowAtIndexPath - numberOfRowsInSection called multiple times but cellForRowAtIndexPath never called 当numberOfRowsInSection等于零时强制调用cellForRowAtIndexPath - Force cellForRowAtIndexPath to be called when numberOfRowsInSection equals to zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM