所以我正在使用 Swift 2 和 Xcode 7 创建一个应用程序,并使用 Parse 作为我的后端服务。 我有两个 View Controller ,一个用于显示 PFObject 列表的 PFQueryTableViewController ,另一个用于显示所选单元格的详细信息。 我认为实现此目的的方法是将唯一的对象 id 附加到数组,然后使用 didSelectRowAtIndexPath 执行 segue。 但我在此处将元素附加到数组时遇到问题。加类后我追加并打印数组,它显示元素 2 次。因此,如果正确的数组是 [1,2,3,4],那么我得到的是 [1,2,3,4,1,2,3,4],真的很奇怪。
var arrayOfGameId = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cellIdentifier = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? PFTableViewCell
if cell == nil {
cell = PFTableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
}
if let object = object {
cell!.textLabel?.text = object["Title"] as? String
cell!.detailTextLabel?.text = object["Platform"] as? String
if let thumbnail = object["Image"]! as? PFFile {
cell!.imageView!.image = UIImage(named: "game1.png")
cell!.imageView!.file = thumbnail
}
let gameid = object["GameId"] as! String!
arrayOfGameId.append(gameid)
}
print(arrayOfGameId)
return cell
}
最佳答案
由于您使用的是 PFQueryTableViewController
,因此无需创建自己的 objectId 列表。
从 queryForTable
返回的 PFObjects
自动存储在名为 objects
的列表中。
如果您需要获取选定的对象,并转到详细 View Controller ,实际上您甚至不需要使用 didSelectRowAtIndexPath
,请尝试以下操作。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController]
var detailsVC = segue.destinationViewController as! DetailsViewController
// Pass the selected object to the destination view controller
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
// selectedObject is the PFObject to be displayed
detailsVC.selectedObject = (objects?[row] as! PFObject)
}
}
关于ios - PFQueryTableViewController 中附加了重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150891/