Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

ios - Swift: Cannot invoke 'reloadData' with no arguments

I'm currently building my first app in Swift. I use Parse as database and want to show all the titles in table Recipes. I created an array for all the titles and add every time a title. But my tableView function doesn't update it (count = 0)

I think that's because I don't reload the tableView after I append a title to my array. But I get aan error if I add self.tableView.reloadData() --> Cannot invoke 'reloadData' with no arguments.

Can someone help me out? I tried so many things and looked everywhere on the internet but couldn't find an answer.

My code:

import UIKit
import Parse
import Bolts

class FirstViewController: UIViewController, UITableViewDelegate {

    var titel = [String]()
    var afbeelding = [UIImage]()
    var afbeeldingFile = [PFFile]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var query = PFQuery(className:"Recipes")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // The find succeeded.
                println("Successfully retrieved (objects!.count) scores.")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {

                        self.titel.append((object["titel"] as! String?)!)

                        println(self.titel)

                        self.tableView.reloadData() //THIS GIVES AN ERROR - Cannot invoke 'reloadData' with no arguments

                    }
                }
            } else {
                // Log details of the failure
                println("Error: (error!) (error!.userInfo!)")
            }
        }

    }

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

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return titel.count

    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell

        cell.gerechtTitel.text = titel[indexPath.row]

        return cell

    }

}

Hope you guys can help me out :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error message is misleading, but the problem is that your FirstViewController does not have a tableView property.

Most probably you want FirstViewController to inherit from UITableViewController instead of UIViewController. That would solve the problem because UITableViewController has a tableView property.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...