简体   繁体   English

UITableView不会加载数据-Xcode 7.2的Swift 2

[英]UITableView doesn't load data - Swift 2 with Xcode 7.2

I'm trying to make a page to show who a user is following in a table. 我正在尝试制作一个页面以显示用户在表中关注的人。 Everything seems pretty clear to me, but for some reasons this code is not loading my data. 对我来说,一切似乎都很清楚,但是由于某些原因,此代码无法加载我的数据。 The following is my following page controller : 以下是我的以下页面控制器:

import Foundation import Parse 导入Foundation导入Parse

class FollowingController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    var profileId : String = String()

    @IBOutlet weak var loadingIndicator: UIActivityIndicatorView!

    @IBOutlet weak var topTitleBar: UIImageView!

    @IBOutlet weak var mainBox: UIImageView!

    @IBOutlet var tableView: UITableView!

    var resultData:NSMutableArray = NSMutableArray()

    func loadData(){

        resultData.removeAllObjects()

        loadingIndicator.hidden = false
        loadingIndicator.startAnimating()

        var counter = 0
        let followingQuery = PFQuery(className: "Followers")
        followingQuery.whereKey("following", equalTo: profileId)
        followingQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if let objects = objects {
                for object in objects {
                    let userQuery = PFUser.query()
                    userQuery?.whereKey("objectId", equalTo: object["followed"] as! String)
                    userQuery?.findObjectsInBackgroundWithBlock({ (users, error ) -> Void in
                        if let users = users {
                            for user in users{
                                let followed = user as! PFUser
                                self.resultData.addObject(followed)
                                counter++
                            }
                        }
                    })
                }
            }

let array:NSArray = self.resultData.reverseObjectEnumerator().allObjects
            self.resultData = NSMutableArray(array: array)
            self.tableView.reloadData()
            if counter == 0 {
                //Handle no results
            }
        }

        loadingIndicator.hidden = true
    }



    override func viewDidLoad() {
        super.viewDidLoad()
    }



    override func viewDidAppear(animated: Bool) {
        self.loadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    }


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

        return resultData.count
    }

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


        let cell:FollowingCell = tableView.dequeueReusableCellWithIdentifier("FollowingCell", forIndexPath: indexPath) as! FollowingCell
        let user:PFUser = self.resultData.objectAtIndex(indexPath.row) as! PFUser

        let name = (user.objectForKey("firstName") as! String) + " " + (user.objectForKey("lastName") as! String)
        cell.name.text = name

        if let profilePicture = user.objectForKey("profilePicture") as? PFFile {
            profilePicture.getDataInBackgroundWithBlock({ (data, error) -> Void in
                if error == nil {
                    let profileImage = UIImage(data: data!)
                    cell.profilePicture.image = profileImage
                }
            })
        }


        return cell
    }

    override func viewDidLayoutSubviews() {
    }

    @IBAction func backButtonPressed(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: {});
    }

}

I created a custom class for my cell and called it FollowingCell. 我为单元格创建了一个自定义类,并将其命名为FollowingCell。 All the outlets are connected to the variables in the class as well as the table datasource & delegate to the ViewController! 所有出口都连接到类中的变量以及表数据源并委托给ViewController! If you spot anything that might be causing this table not to display the data please let me know. 如果发现任何可能导致此表不显示数据的信息,请告诉我。 Thanks. 谢谢。

EDIT : Found out the problem is that even though I load the resultData array in the for loop for each user, outside the findObjectInBackgroundWithBlock call the resultData count returns 0. So it's like if it's not actually updating the array. 编辑:发现的问题是,即使我为每个用户在for循环中加载resultData数组,在findObjectInBackgroundWithBlock调用之外,resultData计数也将返回0。因此,就好像它实际上没有在更新数组一样。 Anybody knows why could that be? 有人知道为什么会这样吗?

You forgot to reload the tableview 您忘了重新加载tableview

mytableview.reloadData()

everytime you load the data you have to reload the full table view or append cells to it.. 每次加载数据时,都必须重新加载完整表视图或向其附加单元格。

And check the cell identifiers declared correctly in storyboard 并检查情节提要中正确声明的单元格标识符

You might forget to set identifier for your Cell from Storyboard as "FollowingCell", double check it. 您可能会忘记将情节提要中的“单元格”标识符设置为“ FollowingCell”,再次检查它。 Also reload the tableView. 还重新加载tableView。

I found the problem, there was a problem when I reloaded the data outside the second query. 我发现了问题,当我在第二个查询之外重新加载数据时出现了问题。 Reloading the data inside the second query call and outside the inner one worked! 在第二个查询调用内和内部查询外部重新加载数据,工作正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM