我已经正确配置基于雷Wenderlich指南发现有动态的行高表视图在这里:
我设置约束以从单元格的顶部到底部具有明确的约束线.我还设置了内容拥抱和内容压缩阻力优先级和估计行高.
这是我用来设置表视图的代码:
func configureTableView() {
// its called on viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0
}
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
for i in 1...20 {
messages.append([
"title": "foo \(i)",
"message": "bla \(i)\nbla\nbla"
])
}
// this is because the actual row heights are not available until the next layout cycle or something like that
dispatch_async(dispatch_get_main_queue(), {self.scrollToBottom(false)})
}
func scrollToBottom(animated:Bool) {
let indexPath = NSIndexPath(forRow: self.messages.count-1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}
Run Code Online (Sandbox Code Playgroud)
这就是我添加新行的方式:
@IBAction func addMore(sender:UIBarButtonItem) {
let message = [
"title": "haiooo",
"message": "silver"]
messages.append(message)
let indexPath = NSIndexPath(forRow: messages.count-1, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
scrollToBottom(true)
}
Run Code Online (Sandbox Code Playgroud)
使用默认行的设置很好.它按预期将行和滚动添加到底部.
但是当我在那之后添加新行时,滚动似乎从最后一个单元格开始.当我添加更多单元格时,偏移量似乎会增加.
这是一个显示它发生的GIF:Imgur
它肯定与滚动动画(不是insertRow动画)有关,因为它在动画关闭时正确滚动.
更改estimatedRowHeight滚动偏移的差异,但我找不到修复它的值.
我也试过延迟使用滚动dispatch_async但它没有改变任何东西.
你们有什么想法吗?
Dav*_*ton 13
哇,这是一个有趣的挑战.感谢您发布测试项目.
因此,似乎在添加新行之后,表视图认为它已滚动到的位置有所不同.在我看来是UIKit中的一个错误.因此,为了解决这个问题,我在应用动画之前添加了一些代码来"重置"表格视图.
这是我最终得到的:
@IBAction func addMore(sender:UIBarButtonItem) {
let message = [
"title": "haiooo",
"message": "silver"]
messages.append(message)
tableView.reloadData()
// To get the animation working as expected, we need to 'reset' the table
// view's current offset. Otherwise it gets confused when it starts the animation.
let oldLastCellIndexPath = NSIndexPath(forRow: messages.count-2, inSection: 0)
self.tableView.scrollToRowAtIndexPath(oldLastCellIndexPath, atScrollPosition: .Bottom, animated: false)
// Animate on the next pass through the runloop.
dispatch_async(dispatch_get_main_queue(), {
self.scrollToBottom(true)
})
}
Run Code Online (Sandbox Code Playgroud)
我无法使用它insertRowsAtIndexPaths(_:withRowAnimation:),但reloadData()工作正常.然后,在动画到新的最后一行之前,您需要再次使用相同的延迟.
| 归档时间: |
|
| 查看次数: |
8434 次 |
| 最近记录: |