import UIKit
import Parse

class AmbianceTableViewTableViewController: UITableViewController,                UISearchBarDelegate, UISearchDisplayDelegate{
    //variables
    var name = [String]()
    var carBrand = [String]()
    var carEngine = [String]()

    //Properties
    @IBOutlet var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        //Query data for Table
        tableView.delegate = self
        tableView.dataSource = self

        let query = PFQuery(className: "Make")
        query.findObjectsInBackground(block: { (objects, error) in
            if error != nil {
                print(error)
            }else{
                if let make = objects {
                    for objects in make {
                        if let make = objects as? PFObject {
                            let name = make["name"] as! String

                            print(make["name"])
                        }
                    }
                }
            }
        })

        self.tableView.reloadData()
    }

    // self.clearsSelectionOnViewWillAppear = false
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    // MARK: - Table view data source

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Ambiance", for: indexPath) as! ViewControllerTableViewCell

        cell.venueNameLabel.text = name[indexPath.row]

        return cell
    }
}

最佳答案

您永远不会在名称数组中添加任何内容,因此tableView将永远不会显示任何数据。同样,查询完成后,您还需要调用reloadData。当前调用是在异步调用之后,这意味着您要在返回数据之前重新加载表。

将查询更改为此,

    let query = PFQuery(className: "Make")
    query.findObjectsInBackground(block: { [weak self] (objects, error) in

        if error != nil {
            print(error)
        }else{
            if let allCarMakes = objects {
                for carMake in allCarMakes {
                    if let make = carMake["name"] as? String {
                        self?.name.append(make)
                    }
                }
            }

        }
        DispatchQueue.main.async {
            self?.tableView.reloadData()
        }
    })

关于swift - 解析表 View 单元格为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42953018/

10-10 20:37