gpt4 book ai didi

ios - 调用 reloadRowsAtIndexPaths 方法时动态单元格高度导致反弹效果

转载 作者:行者123 更新时间:2023-11-29 12:27:46 28 4
gpt4 key购买 nike

背景:在 tableView 中,每个单元格都包含一个显示为图像的按钮。我想展示的效果是,当我点击图片(实际上是按钮)时,按钮的图片会变成另一个图片。
错误:假设有很多数据(超过一页),如果我点击一个单元格的按钮,按钮的图像会改变,但会产生副作用,即整个 tableView 会首先向上滚动一点(像反弹效果)然后向下滚动到以前的地方。
我做了什么:

  1. 在 ViewDidLoad 方法中:
    inboxListTableView.estimatedRowHeight = 60
    inboxListTableView.rowHeight = UITableViewAutomaticDimension
  2. 当我点击按钮时(true 和 false 代表不同的图像和 tableView(cellForRowAtIndex:) 方法中的定义):

    let cell = sender.superview?.superview as InboxListCellTableViewCell
    if let indexPath = inboxListTableView.indexPathForCell(cell) {
    let actionToUpdate = actions[indexPath.row]
    actionToUpdate.imageButton = !actionToUpdate.imageButton.boolValue
    var e: NSError?
    if !MOContext.save(&e) {
    println(e?.description)
    }
    }
  3. 然后在 Fetch Result Controller Delegate 方法中:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    inboxListTableView.beginUpdates()
    }

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
    switch type {
    case .Insert:
    inboxListTableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
    case .Delete:
    inboxListTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    case .Update:
    inboxListTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
    default:
    inboxListTableView.reloadData()
    }
    actions = controller.fetchedObjects as [Action]
    }

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    inboxListTableView.endUpdates()
    }

我认为该错误可能是由两个方面引起的:CoreData 和 Self sizing cells(动态单元格高度)。因为当我使用数组来保存 Action ([Action])而不是 CoreData 时,我没有遇到错误。但是,当我评论 inboxListTableView.rowHeight = UITableViewAutomaticDimension 这段代码时,它没有“弹回/向上滚动”,但单元格的高度是固定的。

有什么想法吗?任何事情都可以提供帮助!谢谢:)

最佳答案

我遇到了完全相同的问题:使用动态单元格高度时出现一点弹跳。我的情况的不同之处在于我没有使用 NSFetchedResultsController 并且我还更改了单元格的高度(展开和折叠它们)。

经过大量试验后,我找到了一个简单的解决方案。只需在 reloadRowsAtIndexPaths 之后调用 scrollToRowAtIndexPath。它对我有用。

tableView.reloadRows(at: [indexPath], with: .none)
tableView.scrollToRow(at: indexPath, at: .none, animated: true)

我在展开和折叠单元格时调用此代码。我希望你能在你的项目中使用它。

关于ios - 调用 reloadRowsAtIndexPaths 方法时动态单元格高度导致反弹效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28574889/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com