我想创建一个tableView的用户从我的Parse数据库是在同一类(在学校)。所有用户都必须拥有用户名,但并不是所有用户都会为其提供全名或设置个人资料图片。我用这个代码:添加项目到特定表格查看行如果可用
let studentsQuery = PFQuery(className:"_User")
studentsQuery.whereKey("objectId", containedIn: studentsArray! as! [AnyObject])
let query2 = PFQuery.orQueryWithSubqueries([studentsQuery])
query2.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error != nil {
// Display error in tableview
} else if results! == [] {
spinningActivity.hideAnimated(true)
print("error")
} else if results! != [] {
if let objects = results {
for object in objects {
if object.objectForKey("full_name") != nil {
let studentName = object.objectForKey("full_name")! as! String
self.studentNameResults.append(studentName)
}
if object.objectForKey("username") != nil {
let studentUsername = object.objectForKey("username")! as! String
self.studentUsernameResults.append(studentUsername)
}
if object.objectForKey("profile_picture") != nil {
let studentProfilePictureFile = object.objectForKey("profile_picture") as! PFFile
studentProfilePictureFile.getDataInBackgroundWithBlock({ (image: NSData?, error: NSError?) in
if error == nil {
let studentProfilePicture : UIImage = UIImage(data: image!)!
self.studentProfilePictureResults.append(studentProfilePicture)
} else {
print("Can't get profile picture")
// Can't get profile picture
}
self.studentsTableView.reloadData()
})
spinningActivity.hideAnimated(true)
} else {
// no image
}
}
}
} else {
spinningActivity.hideAnimated(true)
print("error")
}
}
此代码工作正常,如果所有的用户都有一个用户名,FULL_NAME,和profile_picture。但是,我不知道如何获取用户的用户名的tableView
,并且只有当用户有图片时才将用户的姓名或图片添加到用户的相应tableViewCell
。这里是我的tableView
是如何配置:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return studentUsernameResults.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("studentsCell", forIndexPath: indexPath) as! StudentsInClassInformationTableViewCell
cell.studentProfilePictureImageView.layer.cornerRadius = cell.studentProfilePictureImageView.frame.size.width/2
cell.studentProfilePictureImageView.clipsToBounds = true
cell.studentProfilePictureImageView.image = studentProfilePictureResults[indexPath.row]
cell.studentUsernameLabel.text = studentUsernameResults[indexPath.row]
cell.studentNameLabel.text = studentNameResults[indexPath.row]
return cell
}
的studentProfilePictureResults
,studentUsernameResults
,并提出从解析拉到用户的图片,用户名和姓名的结果阵列studentNameResults
。如果用户没有个人资料图片,我会收到错误Index is out of range
。显然,这意味着有三个名字,三个用户名和只有两个图片,Xcode不知道如何配置单元格。我的问题:如何设置一个用户的用户名的tableView,并将他们的名字和个人资料图片放在同一个单元格中,只要他们有一个?
不要存储单独的数组。存储一个PFObjects数组,我建议你使用[username(string):UIImage)字典作为照片 – Paulw11
@ Paulw11当我查询用户的用户名,名字和图片时,我得到了所有的结果曾经,就像你正在谈论的那样(将结果存储在一个数组中)。我会如何将用户名,姓名和图片存储在一起?我还没有玩过多少字典,但我认为他们只能存储两(2)个结果。我如何储存所有三(3)? –