当当前用户选择一个单元格时,它会转到 ViewController2(VC2),并且需要显示单击标签和 PFImageView 的单元格中的所有用户信息,我有一个 tableView 显示 ViewController1(VC1) 上的所有用户。我有最后一个单元格显示的所有用户信息。因此,即使我单击了不同的单元格,下一个 VC 也仅在列表末尾显示用户。假设我的用户单元列表显示以 Aisling、roger、dave 和 john 开头。我单击单元格“Aisling”并转到 VC2,它显示了 Johns 的所有个人资料信息。
VC1: 类下:
var appUsers = [PFUser]()
var userToShow = PFUser()
然后:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let singleCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("mySingleCellid") as! CustomCell
let userObject = appUsers[indexPath.row] as PFObject
singleCell.userName.text = userObject["name"] as? String
singleCell.userAge.text = userObject["age"] as? String
singleCell.usersProfileImage.file = userObject["image"] as? PFFile
singleCell.usersProfileImage.loadInBackground()
// when userToShow is here it shows the user detail of the last cell everytime
userToShow = appUsers[indexPath.row]
return singleCell
} // cell for row
// when userToShow is here it crashes the app when the cell is clicked giving the error:unexpectedly found nil while unwrapping an Optional value
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
userToShow = appUsers[indexPath.row]
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "userProfileDetailsSegue" {
if let destinationVC = segue.destinationViewController as? UserProfileDetailsViewController {
destinationVC.userToShowDetail = userToShow
}
}
}
}
在 VC2 上: 在类下面:
var userToShowDetail = PFUser()
然后:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
userName.text = userToShowDetail["name"] as? String
userBio.text = userToShowDetail["bio"] as? String
userAge.text = userToShowDetail["age"] as? String
userProfile.file = userToShowDetail["image"] as? PFFile
userProfile.loadInBackground()
}
它只在 VC2 中显示属于 VC1 最后一个单元格中的用户的用户详细信息。而不是属于所选单元格中用户的用户详细信息。
现在坚持了几个星期,我的截止日期正在推进无法解决它任何帮助将不胜感激!!!我可以在网上找到的所有链接和帮助都是针对 C# 和 obj c 的。
最佳答案
VC1
//PUT THIS IN THE FIRST VC CLASS
var appUserResult: PFUser?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//TAKE OUT LET - YOU'VE ALREADY DEFINED AS CLASS VAR
appUserResult = appUsers[indexPath.row]
print(appUserResult)
self.performSegueWithIdentifier("openVC2", sender: self)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "openVC2" {
let indexPath = self.resultsPageTableView.indexPathForSelectedRow!
//INDEXPATH ISN'T AVAILABLE IN PREPAREFORSEGUE
//let userToShow = appUsers[indexPath.row]
let newVc = segue.destinationViewController as! UserProfileDetailsViewController
//USE THE CLASS VARIABLE TO PASS
newVc.userToShowDetail = appUserResult
VC2
var userToShowDetail: PFUser?
在 Storyboard中,删除旧的 segue。从 VC1 到 VC2(不是从表格单元格)创建一个新的。为其指定标识符“openVC2”。
关于ios - 将单元格信息从一个 View Controller 发送到另一个 Swift 和 Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769902/