重新加载表视图产生0行,基于"空"尽管数组被追加?

时间:2017-08-19 04:30:16

标签: arrays swift tableview

我有一个设置,我在viewController的viewDidLoad()中运行查询。 VC包含tableView。查询从数据库中提取信息,然后在完成后重新加载表视图。这是一般设置:

class DetailDisclosureVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    struct Comment {
        var objectID: String
        var userID: String
        var userName: String
        var commentString: String
        var createdAt: Date

        init(objectID: String, userID: String, userName: String, commentString: String, createdAt: Date) {
            self.objectID = objectID
            self.userID = userID
            self.userName = userName
            self.commentString = commentString
            self.createdAt = createdAt
        }

    }

    var orderedComments = [Comment]()

    @IBOutlet weak var commentsTableView: UITableView!

    func query(){
        let commentsQuery = PFQuery(className: "Comments")
        commentsQuery.findObjectsInBackground { (objectss, error) in
            if let objects = objectss{
                print("The objects.count is \(objects.count)") //returns 2
                if objects.count != 0{
                    for object in objects{

                        let individualOrderedComment = Comment(objectID: object.objectId!, userID: object["userID"] as! String, userName: object["userName"] as! String, commentString: object["commentString"] as! String, createdAt: object.createdAt!)

                        self.orderedComments.append(individualOrderedComment)
                    }
                    print(orderedComments.count) //this ALWAYS returns 2, as intended
                    commentsTableView.reloadData()
                }
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        commentsTableView.dataSource = self
        commentsTableView.delegate = self
        query()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //number of comments
        print("orderevents count\(orderedComments.count)") //ALWAYS returns 0
        if orderedComments.count != 0{
            return orderedComments.count
            print("Wasnt 0")
        }else{
            return 1
            print("It was 0")
        }
    }

}

我确信查询有效并且orderedComments数组中有两个值。我不能为我的生活找出原因,即使在查询结束时调用.reloadData()之后,numberOfRowsinSection继续为orderedComments.count返回值0,因此返回0行。我已经玩过添加和删除self.的某些变量等等......但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

findObjectsInBackground()调用其完成闭包的线程是什么?我打赌它不是主线。您最好将封闭代码放在DispatchQueue.main.async中,如下所示:

DispatchQueue.main.async 
{
   if let objects = objectss
   {
        print("The objects.count is \(objects.count)") //returns 2
        if objects.count != 0
        {
            for object in objects
            {
                let individualOrderedComment = Comment(objectID: object.objectId!, userID: object["userID"] as! String, userName: object["userName"] as! String, commentString: object["commentString"] as! String, createdAt: object.createdAt!)

                self.orderedComments.append(individualOrderedComment)
            }
            print(orderedComments.count) //this ALWAYS returns 2, as intended
            commentsTableView.reloadData()
        }
    }
}

否则你的reloadData就不会起作用(假设我对它所关注的主题是正确的。)