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

时间:2015-08-23 11:46:57

标签: swift parse-platform uiviewcontroller uicollectionview segue

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

我已经生成了所有用户的列表,并将每个用户放在他们自己的Cell中。

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)
}

在我的详细控制器中,我设置了信息检索。

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的返回值始终为nil,OtherUserViewController的ViewDidLoad也是如此。

我做错了什么,我怎样才能正确设置segue?

1 个答案:

答案 0 :(得分:0)

基本上你正在做的事情在逻辑上很好,并且segues没有问题。但问题是:

  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,否则您可能会丢失其他可能在以后需要的关联属性。如果是这种情况我会建议将整个PFObject存储在数组中并使用它,这样你就不必再次调用Parse但你会使用一点内存。

但是你的代码中也存在潜伏的危险。不要从单独的线程调用UI更新。如果我正确,正在从一个单独的线程执行解析块,建议在主线程上发布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()
                       })

                    }
                }
            }
    }
})

如果有帮助,请告诉我。干杯!