好的,所以我一直在尝试为我正在开发的应用程序制作一个赞按钮,并且我正在使用 parse 作为后端。到目前为止,我可以更新解析中的like按钮,但是,我只能重新加载整个表格视图,而不仅仅是单个单元格。重新加载整个 tableview 有时会导致 tableview 向上或向下移动我相信是因为不同大小的单元格。
@IBAction func topButton(sender: UIButton) {
let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: hitIndex!) as TableViewCell
//var indexOfCell:NSIndexPath
//indexOfCell = hitIndex!
if object.valueForKey("likedBy") != nil {
var UUIDarray = object.valueForKey("likedBy")? as NSArray
var uuidArray:[String] = UUIDarray as [String]
if !isStringPresentInArray(uuidArray, str: uuid) {
object.addObject(uuid, forKey: "likedBy")
object.incrementKey("count")
object.saveInBackground()
//self.tableView.reloadData()
}
}
}
我已经用正确的 indexPath 尝试了 cellForRow,它总是返回 PFTableViewCell 不包含 cellForRowAtIndexPath 方法。我在论坛上读到有人能够创建自定义方法来重新加载单元格,Parse 的文档使用 PAPCache 来执行类似按钮。幸运的是,Parse 页面上类似按钮的文档位于 Obj-C 中,并且我已经用 Swift 编写了我的应用程序。如果您想查看,这是该页面的链接:https ://www.parse.com/tutorials/anypic#like 。第 6.1 节。
TD;LR我不确定如何更新单元格,以便在没有 cellForRowAtIndexPath 方法的情况下不会重新加载整个表格视图。也许自定义方法?也许是 PAPCache 方法?
解决了
if object.valueForKey("likedBy") != nil {
var UUIDarray = object.valueForKey("likedBy")? as NSArray
var uuidArray:[String] = UUIDarray as [String]
if !isStringPresentInArray(uuidArray, str: uuid) {
object.addObject(uuid, forKey: "likedBy")
object.incrementKey("count")
object.saveInBackground()
let count = object.valueForKey("count") as Int?
//cell.count.text = "\(count)"
self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.None)
这只会正确地重新加载单元格,但是,现在我的整个表格视图总是滚动到顶部,这并不理想。