错误:UITableView使用UITableViewAutomaticDimension跳到顶部 [英] Error: UITableView jump to top with UITableViewAutomaticDimension

查看:97
本文介绍了错误:UITableView使用UITableViewAutomaticDimension跳到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITableView estimatedRowHeight UITableViewAutomaticDimension 。我也使用 NSFetchedResultsControllerDelegate 反映更改。

I am using UITableView with estimatedRowHeight and UITableViewAutomaticDimension. Also I am using NSFetchedResultsControllerDelegate to reflect the changes.

一切正常。现在的问题是,每当我将某些记录添加到 CoreData NSFetchedResultsController 时,都会将其称为委托,但是会发生意外情况。 TableView每次都会突然滚动到Top。

Everything is work fine. Now the problem is when ever I add some record to CoreData, NSFetchedResultsController called the it's delegate but an unexpected things happen. TableView suddenly scroll to Top every time.

NSFetchedResultsControllerDelegate

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

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

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
        switch type {
        case .Insert:
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
            break

        case .Update:
            tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
            break

        case .Delete:
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
            break

        default: break;
        }
    }

通过谷歌搜索,我发现很少答案,人们建议使用 tableView: heightForRowAtIndexPath:但由于我的单元格高度是动态的。所以我该怎么做?

By googling I found few answers where people suggested to use tableView: heightForRowAtIndexPath: but as my cell height is dynamic. So what should I do?

推荐答案

这是tableview的默认行为,在tableview中插入一行将其滚动到顶部。
我的应用确实遇到了同样的问题。
这是我如何管理tableview的内容偏移量,请按照以下步骤操作:

This is default behaviour of tableview, inserting an row in tableview scroll it to top. I did faced the same issue with my app. Here is how i able to manage the content offset of tableview, follow the steps,

1)在插入行或节之前存储UITableview的内容偏移量。

1)Store the content offset of UITableview before inserting row or section.

2)将对象插入数组。

2)Insert objects in array.

3)重新加载表格视图

3)Reload the tableview

4)减去差并手动设置内容偏移。

4)Subtract the difference and set the content offset manually.

let oldOffset: CGFloat = tblTest.contentSize.height
tblTest.reloadData()
let newOffset: CGFloat = tblTest.contentSize.height
tblTest.setContentOffset(CGPointMake(0, (newOffset - oldOffset)), animated: false)

到目前为止,这是我在应用程序中所做的工作,并且使用动态单元格来克服每次重新初始化单元格的问题,它的功能就很棒。

This is how i have done in my applications so far, and with dynamic cell to overcome the reinitialise cell everytime, it is working just awesome.

这篇关于错误:UITableView使用UITableViewAutomaticDimension跳到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆