将 PFUsers 添加到 tableView 仅加载一个用户?
Posted
技术标签:
【中文标题】将 PFUsers 添加到 tableView 仅加载一个用户?【英文标题】:Adding PFUsers to a tableView only loads one user? 【发布时间】:2015-04-12 11:09:33 【问题描述】:我正在从 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
的地方,尽管它只加载一个用户,而不是我查询的所有用户。
【问题讨论】:
【参考方案1】:看起来您在循环中(在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))
【讨论】:
是的!杰出的!我是新人,我很接近,非常感谢您以上是关于将 PFUsers 添加到 tableView 仅加载一个用户?的主要内容,如果未能解决你的问题,请参考以下文章
如何将 Firebase 数据检索到 TableView android 中?我应该使用 RecycleView 还是仅适用于列表?
将 tapGesture 添加到 tableView 则无法执行 tableView(_ tableView: UITableView, didSelectRowAt indexPath: Index