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

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

问题描述

我正在使用 UITableViewestimatedRowHeightUITableViewAutomaticDimension.此外,我使用 NSFetchedResultsControllerDelegate 来反映更改.

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

一切正常.现在的问题是,当我向 CoreData 添加一些记录时,NSFetchedResultsController 会调用它的委托,但会发生意想不到的事情.TableView 每次都会突然滚动到顶部.

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)重新加载tableview

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天全站免登陆