0

I have a query when ViewDidLoad and i want to pass it to function tableview so i can use it to write text into a label like cell.textCell.text

How can i do that? My code is showing me blank rows.

import UIKit
import Parse

class ListaTableViewController: UITableViewController {

var queryArray: [PFObject] = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

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

        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) Restaurantes.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                if let objects = objects as? [PFObject] {
                    if let pfObject: PFObject = objects as? PFObject {

                        self.queryArray.append(pfObject as PFObject)

                    }


                    //     println(object.objectId)

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



}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return queryArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ListaTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ListaTableViewCell
    println("hey")

    cell.textCell.text = queryArray[indexPath.row]["nome"] as? String

    cell.imageBg.image = UIImage(named: "www.maisturismo.jpg")


    return cell
}

}

1 Answer 1

2

Just call self.tableView.reloadData()

import UIKit
import Parse

class ListaTableViewController: UITableViewController {

var queryArray: [PFObject] = [PFObject]()

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

        if error == nil {
            println("Successfully retrieved \(objects!.count) Restaurantes.")
            if let _objects = objects as? [PFObject] {
                self.queryArray = _objects
                self.tableView.reloadData()
            }
        } else {
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
}

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ListaTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ListaTableViewCell
    let restaurante = queryArray[indexPath.row] as! PFObject
    cell.textCell.text = restaurante.objectForKey("nome") as! NSString
    cell.imageBg.image = UIImage(named: "www.maisturismo.jpg")

    return cell
}
Sign up to request clarification or add additional context in comments.

9 Comments

Same. White rows... I dont know what to do... Im trying to fix this for hours. The problem is here: cell.textCell.text = queryArray[indexPath.row]["nome"] as? String
Maybe your problem is ListaTableViewCell or queryArray[indexPath.row]["nome"] as? String is nil.. You need to check queryArray.count has the correct size.. If yes, your problem is with your cell.
Dont return anything. =/
wthat it prints? println("Successfully retrieved \(objects!.count) Restaurantes."). objects!.count is more than 0?
For images you have other workaround. Look at this: stackoverflow.com/a/29730286/846780
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.