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

标签 swift parse-platform uiviewcontroller uicollectionview segue

我正在构建一个社交网络应用程序,目前我想在 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/

相关文章:

ios - 如何以编程方式在 Swift 中使用 UISlider 更改 UIImageView 中指定的图像大小

ios - 创建单位转换器的最有效方法是什么,用户可以滚动浏览两个单位选项列表(并排)

swift - 自定义 MKAnnotationView 想要再次被点击

javascript - 如何在Parse中将不同表的两个查询结果合并为一个查询?

ios - 无法从 AppDelegate 调用 ViewController 方法

cocoa-touch - 如何为 SCNNode 上的自定义属性设置动画?

ios - 在 Parse 中按降序显示最后 30 条记录

java - Android - 无法从匿名调用的方法获取 URI

swift - 在 swift 4 中为 UINavigationController 和 UIViewController 禁用屏幕方向

ios - 如何在 Swift 中正确实现 UIViewController 和 UIView 之间的协议(protocol)-委托(delegate)模式