- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个社交网络应用程序,目前我想在 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/
首先,我使用 Storyboard。我的 ViewController 层次结构如下 NavigationController -> SplashScreen -> LoginScreen -> Ma
我有一个 UISplitview Controller (带有主视图和详细 View )。所以,当 detailview加载 viewDidLoad调用名为 [self animateToPositi
好的,所以我有一个主详细信息应用程序,当用户单击某个单元格时,它会在 DetailViewController 中加载网页。问题是,在 iPhone 版本(这是一个通用应用程序)上,我必须单击单元格,
我做了一个 MasterDetailView 应用程序。在 TableView 中,我有员工列表,在 detailView 中,我有一个欢迎屏幕,其中包含员工详细信息和其他详细信息。当我点击任何员工时
我创建了一个 NavigationBar 和 DetailView,但是当我在 tableView 上选择一行时,没有设置 DetailView 的标题。如何设置? 谢谢! 最佳答案 如果您在 UIN
我正在为一个主从应用程序使用苹果模板,我想知道:如果我在 iPad 或任何其他具有常规水平尺寸类的设备上显示内容。当我打开 MasterViewController 时,DetailViewContr
我的代码没有错误,但是细节 Viewcontroller 上的数组没有接收到我通过 segue 发送给它的对象。我知道我的数组充满了对象,因为我可以将它们打印到日志中.我可以在准备转场之前对它们进行计
我有一个 UITableview,我用数据库中的数据填充了它,我知道将什么数据发送到另一个 View (detailViewController),但是我无法让它工作,现在我正在通过 - (void)
我有一个主 TableView 和一个详细 View 。当我单击单元格详细信息 View 时,应显示该项目的详细信息。现在我想做的是创建 subview 并将该 subview 放入详细 View 中
我正在使用 UISplitViewController 开发 iPad 应用程序。我还使用 DetailViewController 在其中显示一些来自 XML 的信息,但是这些信息没有显示在 Det
我很难选择显示为 Split View Controller 中的详细 View 的 View Controller 。似乎在 showDetail 后面有一些 View Controller 的默认
我已经查看了互联网上的每个答案,但它一直给出错误。我知道这可能会重复,但解决方案对我不起作用。 我已将 detailViewController 设为公共(public)类 我添加了 detailVi
我开始使用 Storyboard,但注意到一个非常显着的区别:每次我来回导航时, Storyboard似乎都会实例化一个新的 ViewController。示例:我基于 Master-Detail 模
我正在尝试创建一个 iPad 应用程序,其用户界面与 Apple 的邮件应用程序类似,即: RootView Controller ( TableView )位于 Split View的左侧,用于通过
我正在 iPad 上开发这个应用程序。我的 MainWindow 包含一个 Split View Controller ,它加载 RootViewController 和 DetailViewCont
目前,我有一个带有 MasterViewController 和 DetailViewController 的 SplitViewController。我想知道是否有办法拥有更多 DetailView
因此,使用下面的代码,我将我的 CollectionViewCell indexPath 插入到新的 DetailViewController 中。现在每个“DetailViewController”
我正在尝试创建一个 master-DetailView 应用程序,但目前我陷入困境,我正在尝试为我的数据创建模板。例如,我想要一个数组来手动控制数据(例如标签/图像)并通过索引路径进行更改,因此每个单
我正在我的应用程序中实现 SplitViewController。我的问题是,当我在较小的设备(例如 iphone5)上显示此内容时,我的 SplitViewController 的 masterVi
这个问题已经有答案了: Passing data between view controllers (44 个回答) 已关闭 4 年前。 我的模型中有一个名为 的字典 var detailStock:
我是一名优秀的程序员,十分优秀!