iosswiftuitableviewpfquery

Display column data in tableview cell - Why aren't my table cells being created and populated?


When I build and run, my table is empty. I have verified my connection with the database. When I hard code the data, it works just fine. I assume my code implementation is incorrect as this is the first time I've used Parse.

import UIKit

class ListSubViewController: UITableViewController {
    var vendorArray : NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        let query = PFQuery (className: "Vendor")
        query.order(byAscending: "ID")
        query.findObjectsInBackground {
            (objects:[PFObject]?, error:Error?) -> Void in

            if error == nil {
                if let objects = objects {

                    print("\(objects.count) vendors are listed")
                    for object in objects {
                        self.vendorArray.add(object)
                    }
                    self.tableView.reloadData()
                }
            } else {
                print("There is an error")
            }
        }
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

        let vendorObjects = vendorArray[indexPath.row] as! PFObject
        let vendorName = vendorObjects["vendorName"] as! String
        let image = vendorObjects["Image"] as! PFFile

        cell.vendorLabel.text = vendorName
        image.getDataInBackground(block: {
            (result, error) in
            cell.vendorImage.image = UIImage(data: result!)
        })

        self.tableView.reloadData()
        return cell
    }

Solution

  • Putting this line

    self.tableView.reloadData()
    

    inside cellForRowAt causes a recursive - endless process as the table is reloaded for every cell creation