Arrays 将PFUsers添加到tableView仅加载一个用户?

Arrays 将PFUsers添加到tableView仅加载一个用户?,arrays,uitableview,swift,parse-platform,Arrays,Uitableview,Swift,Parse Platform,我正在从Parses后端数据库查询一组PFUsers。然后我想将这些用户放在TableView中,尽管出于某种原因,它只是出于某种原因加载查询的第一个用户。有人能帮忙吗,代码: struct UserMatches { var finalMatchesName : String var finalMatchesAge : Int var finalMatchesLocation : PFGeoPoint var finalMatchesImage : NSDat

我正在从Parses后端数据库查询一组PFUsers。然后我想将这些用户放在TableView中,尽管出于某种原因,它只是出于某种原因加载查询的第一个用户。有人能帮忙吗,代码:

struct UserMatches {

    var finalMatchesName : String
    var finalMatchesAge : Int
    var finalMatchesLocation : PFGeoPoint
    var finalMatchesImage : NSData
}

        class Matches: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate {

            var user = PFUser.currentUser()

            var tableView = UITableView()

            var userMatches = [UserMatches]()

            override func viewDidLoad() {
                super.viewDidLoad()
                matchedUsers()
                createTableView()

            }

            func createTableView() {

                tableView.frame = CGRectMake(0, 10, self.view.frame.width, self.view.frame.height - 10)
                tableView.dataSource = self
                tableView.delegate = self
                tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        }

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

            return self.userMatches.count

        }

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

            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

            let userDetails = self.userMatches[indexPath.row]

            //Name
            var nameLabel = UILabel(frame: CGRectMake(15, 3, cell.frame.width / 1.2, cell.frame.height / 2))
            nameLabel.font = UIFont(name: Font.FuturaBlack, size: 20)
            nameLabel.numberOfLines = 1
            nameLabel.text = userDetails.finalMatchesName
            nameLabel.adjustsFontSizeToFitWidth = true
            nameLabel.textAlignment = NSTextAlignment.Left

            //Distance
            var distanceLabel = UILabel(frame: CGRectMake(15, nameLabel.frame.height + 3, cell.frame.width / 1.2, cell.frame.height / 3))
            distanceLabel.font = UIFont(name: Font.FuturaMedium, size: 16)
            distanceLabel.numberOfLines = 1
            distanceLabel.text = "0.25 Miles Away"
            distanceLabel.adjustsFontSizeToFitWidth = true
            distanceLabel.textAlignment = NSTextAlignment.Left

            //image
            var imageView = UIImageView(frame: CGRectMake(cell.frame.origin.x + cell.frame.width - cell.frame.height - 5, 5, cell.frame.height - 10, cell.frame.height - 10))
            let image = UIImage(named: "hot.png")
            var images = UIImage(data: userDetails.finalMatchesImage)
            imageView.image = images
            imageView.layer.cornerRadius = imageView.frame.size.width / 2
            imageView.clipsToBounds = true
            imageView.contentMode = .ScaleAspectFill


            cell.addSubview(imageView)
            cell.addSubview(distanceLabel)
            cell.addSubview(nameLabel)

            return cell
        }

func matchedUsers() {

            //Query Matches
            var matchesIdQuery = PFUser.query()
            matchesIdQuery.whereKey("objectId", equalTo: user.objectId)
            if var result = matchesIdQuery.getFirstObject() {

            let matchesId = result["matches"] as [String]

                let matchesQuery = PFUser.query()
                matchesQuery.whereKey("objectId", containedIn: matchesId)
                let finalResult = matchesQuery.findObjects()
                    if finalResult.count > 0 {

                        for finalMatches in finalResult {

                            let matchesImageFile = finalMatches["image"] as PFFile
                            if let finalMatchesImageFile = matchesImageFile.getData() {

                                println(finalMatches.count) //Prints 2

                                self.userMatches = [UserMatches(finalMatchesName: finalMatches.username, finalMatchesAge: finalMatches["age"] as Int, finalMatchesLocation: finalMatches["location"] as PFGeoPoint, finalMatchesImage: finalMatchesImageFile)]

                                println(self.userMatches.count) //Prints 1

                                self.tableView.reloadData()
                            }
                        }
                    }
                }
            }
}


问题是,我试图在
matchedUsers
函数中加载
UserMatches
,尽管它只加载一个用户,而不是我查询的所有用户。

看起来您在循环中为数组(在
matchedUsers()
中)分配一个值,而不是附加到它

尝试替换:

self.userMatches = [UserMatches(finalMatchesName: finalMatches.username, finalMatchesAge: finalMatches["age"] as Int, finalMatchesLocation: finalMatches["location"] as PFGeoPoint, finalMatchesImage: finalMatchesImageFile)]
与:


对明亮的我和你很亲近,非常感谢
self.userMatches.append(UserMatches(finalMatchesName: finalMatches.username, finalMatchesAge: finalMatches["age"] as Int, finalMatchesLocation: finalMatches["location"] as PFGeoPoint, finalMatchesImage: finalMatchesImageFile))