Ios 使用未解析标识符';objectAtIndexPath';
我正在开发一个允许你“喜欢”帖子的应用程序。我正在实现like按钮,但我遇到了一个似乎无法修复的错误。 我在其他帖子中搜索过,但我不确定如何修复它 这是我用来实现like按钮的代码。我是否需要将某些内容导入到我的项目中?或者在某一点打开 任何帮助都是非常感谢的Ios 使用未解析标识符';objectAtIndexPath';,ios,iphone,swift,cocoa-touch,parse-platform,Ios,Iphone,Swift,Cocoa Touch,Parse Platform,我正在开发一个允许你“喜欢”帖子的应用程序。我正在实现like按钮,但我遇到了一个似乎无法修复的错误。 我在其他帖子中搜索过,但我不确定如何修复它 这是我用来实现like按钮的代码。我是否需要将某些内容导入到我的项目中?或者在某一点打开 任何帮助都是非常感谢的 override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
let cell:PostTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! PostTableViewCell
let post = self.objectAtIndexPath(indexPath!) as PFObject
cell.postTextView.alpha = 0
cell.usernameLabel.alpha = 0
cell.timestampLabel.alpha = 0
cell.postTextView.text = post.objectForKey("content") as! String
var dataFormatter:NSDateFormatter = NSDateFormatter()
dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
cell.timestampLabel.text = dataFormatter.stringFromDate(post.createdAt!)
// to get username from the post
var showUsername:PFQuery = PFUser.query()!
//the objectID is the same as the user in the two different tables
showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!.objectId!!)
showUsername.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil{
let user = (objects as! [PFUser]).last
cell.usernameLabel.text = user!.username
UIView.animateWithDuration(0.5, animations: {
cell.postTextView.alpha = 1
cell.usernameLabel.alpha = 1
cell.timestampLabel.alpha = 1
})
}
}
return cell
}
func objectAtIndexPath(indexPath: NSIndexPath) -> PFObject {
return self.timelineData[indexPath.row] as! PFObject
}
@IBAction func likeButton(sender: UIButton) {
//disables the like button so it can't be pressed again
sender.enabled = false
sender.userInteractionEnabled = false
sender.alpha = 0.5
//get the point in the table view that corresponds to the button that was pressed
//in my case these were a bunch of cells each with their own like button
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = self.objectAtIndexPath(hitIndex!) as PFObject
//this is where I incremented the key for the object
object.incrementKey("likes")
object.saveInBackground() //still gives me error here
self.tableView.reloadData()
NSLog("Top Index Path \(hitIndex?.row)")
}
更新2:添加了错误的照片
由于视图控制器既不是
NSFetchedResultsController
也不是PFQueryTableViewController
,因此您必须自己实现objectAtIndexPath:
有关所需代码的提示位于CellForRowatineXpath'中:
let post:PFObject = self.timelineData.objectAtIndex(indexPath!.row) as! PFObject
不使用数组上的objectAtIndex方法,只需在行处索引到数组中:
func objectAtIndexPath(indexPath: NSIndexPath) -> PFObject {
return self.timelineData[indexPath.row] as! PFObject
}
无论旧代码出现在什么地方(在likeButton中),都可以调用它
或者,在CellForRowatineXpath中:
let post = self.objectAtIndexPath(indexPath) as!PFObject
等等。objectAtIndexPath应该是什么?它返回一个位于特定indexPath的对象,在本例中是
hitIndex
,因为每个用户帖子中都有like按钮,因此,它只为特定的帖子选择了“like”为什么要使用问号?为了清楚起见-这个类是从NSFetchedResultsController
继承的吗?我找不到具有objectAtIndexPath
方法的任何其他类。如果不是,您是否提供了此方法的实现?如果是,请显示。本来没有,但仍然给我一个错误,所以有时我会通过展开来修复错误,但这次不起作用。我将对其进行编辑以将其还原为原始。return“那”是指从tableView
函数返回objectAtIndexPath
?或者?是的<代码>返回self.timelineData.objectAtIndex(indexPath!.row)好的,我这样做了,但是我仍然有错误,我对我正在做的事情有点困惑是的-我不是很熟悉swift,但是我没有在swift中看到数组上的objectAtIndexPath方法。但是这个想法和索引到它是一样的。请参阅修改答案的编辑。好的,我试过了,它成功了,错误消失了。我不得不将其更改为:return self.timelineData[indexPath.row]as!PFObject
否则它会给我一个错误,但现在,它没有在后台保存类似的内容。我收到一个错误,object.saveInBackground()
没有名为saveInBackground()
let post = self.objectAtIndexPath(indexPath) as!PFObject