我正在从 Parses 后端数据库查询一组 PFUser。然后我想将这些用户放入 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))
关于arrays - 将 PFUsers 添加到 tableView 只会加载一个用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29588745/