gpt4 book ai didi

swift - 使用 UICollectionViewCell 中选定的 PFUser 内容更新 DetailViewController

转载 作者:行者123 更新时间:2023-11-30 14:07:39 24 4
gpt4 key购买 nike

我正在构建一个社交网络应用程序,目前我想在 PFUser.CurrentUser() 从 UICollectionView 中选择另一个用户的 Cell 时在 HomeViewController 和 OtherUserViewController 之间创建一个 segue。 segue 工作正常,但 otherUserName 标签没有更新所选 PFUser 的名称。

我生成了所有用户的列表,并将每个用户放入自己的单元格中。

var userListData = [String]()
var userListUserNames = [String]()

@IBOutlet weak var userCollectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

userCollectionView.delegate = self
userCollectionView.dataSource = self
loadData()
}
func loadData (){
var userListQuery = PFUser.query()
userListQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {

if let objects = objects {

for object in objects {

if let user = object as? PFUser {

if user.objectId != PFUser.currentUser()?.objectId {

self.userListData.append(user.objectId!)
self.userListUserNames.append(user.username!)
self.userCollectionView.reloadData()
}
}
}
}
}
})
}

之后我创建了单元格。

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return userListData.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("homeCollectionViewCell", forIndexPath: indexPath) as! HomeCollectionViewCell

var names = self.userListUserNames[indexPath.row]
cell.profileNameButtonOutlet.setTitle(names.lowercaseString, forState: UIControlState.Normal)

return cell
}

然后我为单元格创建了一个prepareForSegue方法和didDeselectItemAtIndexPath。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var currentOtherUser: PFObject?
if let object = sender as? PFObject {

currentOtherUser = sender as? PFObject
} else {
println("there is no currentOtherUser")
}
var otherUserScene = segue.destinationViewController as! OtherUserViewController
otherUserScene.currentOtherUser = (currentOtherUser)
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let currentOtherUser = userListUserNames[indexPath.row]
self.performSegueWithIdentifier("homeToOtherUser", sender: currentOtherUser)
}

在我的详细 Controller 中,我设置了信息检索。

class OtherUserViewController: UIViewController {

var currentOtherUser : PFObject?

@IBOutlet weak var otherUserName: UILabel!

var otherUser: PFUser?

override func viewDidLoad() {
super.viewDidLoad()

if let object = currentOtherUser {
if let value = object["username"] as? String {

otherUserName.text = value

}
} else {
println("no name to display")
}
}
}

prepareForSegue 的返回值始终为零,OtherUserViewController 的 ViewDidLoad 也是如此。

我做错了什么以及如何正确设置segue?

最佳答案

基本上,您所做的事情在逻辑上是没有问题的,而且转场没有问题。然而问题是:

  var userListUserNames = [String]()   // This is not [PFOBject]

但是在准备segue时,您正在检查:

 if let object = sender as? PFObject {
//which will never be called because sender is of type <String>
currentOtherUser = sender as? PFObject
} else {
println("there is no currentOtherUser") //this is what is being displayed right now
}

这是因为:

  func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let currentOtherUser = userListUserNames[indexPath.row] //This is String
self.performSegueWithIdentifier("homeToOtherUser", sender: currentOtherUser)

}

这就是问题所在。

解决方案是要么将类型设置为 String,如果您这样做,您可以获得名称,但您可能会丢失稍后可能需要的 otherUser 的其他关联属性。如果是这种情况,我建议将整个 PFObject 存储在数组中并使用它,这样您就不必一次又一次地调用 Parse,但会使用一点内存。

但是您的代码中也潜伏着危险。不要从单独的线程调用 UI 更新。如果我是正确的,则解析 block 正在从单独的线程执行,建议在主线程上发布 UI 更新,如下所示:

userListQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil && objects != nil{
for object in objects! {

if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId {

self.userListData.append(user.objectId!)
self.userListUserNames.append(user.username!)

dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.userCollectionView.reloadData()
})

}
}
}
}
})

请告诉我这是否有帮助。干杯!

关于swift - 使用 UICollectionViewCell 中选定的 PFUser 内容更新 DetailViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32166301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com