同じクラス(学校)にある私のParseデータベースからユーザーのtableViewを作成しようとしています。すべてのユーザーはユーザー名を持っている必要がありますが、すべてのユーザーにそのフルネームを与えたり、プロフィール画像を設定したりするわけではありません。私はこのコードを使用しています:特定のテーブルにアイテムを追加する利用可能であれば行を表示する
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")
}
}
このコードは、すべてのユーザーがusername、full_name、およびprofile_pictureを持っていれば正常に動作します。しかし、ユーザーのユーザー名のtableView
を取得し、ユーザーの名前または画像をユーザーの対応するtableViewCell
に追加する方法を把握することはできません。パースから引き出さユーザーの画像の配列、ユーザ名、および名前の結果から来
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
:ここに私のtableView
が設定されている方法です。ユーザーがプロフィール写真を持っていない場合は、エラーIndex is out of range
が表示されます。明らかに、これは、3つの名前、3つのユーザー名、2つのピクチャしか存在せず、Xcodeはセルの構成方法を知らないことを意味します。私の質問:テーブルビューを設定してユーザーのユーザー名を設定し、名前とプロフィールの画像を同じセルに配置するにはどうすればよいですか?
別々のアレイを保管しないでください。 PFObjectsの1つの配列を格納すると、写真の[username(string):UIImage]辞書を使用することをお勧めします – Paulw11
@ Paulw11ユーザーのユーザー名、名前、画像を照会すると、一度、あなたが話しているように(結果を1つの配列に格納する)。ユーザー名、名前、画像を一緒に保存するにはどうすればいいですか?私は辞書ではあまりプレイしていないが、2つの結果しか保存できないと思った。どのようにして3つすべてを保存できますか? –