ios - 如何将最后一行添加到 UITableView 并平滑滚动?

标签 ios swift uitableview uiscrollview

我正在使用 UITableView 构建聊天界面。我使用下面的代码添加单元格

self.tableView.beginUpdates()
self.tableView.insertRows(at: [IndexPath(row: self.messageCards.count - 1, section: 0)], with: .bottom)
    self.tableView.endUpdates()

添加后,我使用scrollToRowAtIndexPath来显示最后插入的行。

let indexPath = IndexPath(row: self.messageCards.count - 1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)

但最后两个动画都混合并且滚动不顺利。

最佳答案

你可以试试这个,我已经测试过代码:

    arrayData.addObject(str)
    let indexpath = NSIndexPath(forRow: arrayData.count-1, inSection: 0)

    tblView.insertRowsAtIndexPaths([indexpath], withRowAnimation: UITableViewRowAnimation.Automatic)
    tblView.scrollToRowAtIndexPath(indexpath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

以下是输出:如果您想要完整的源代码,请告诉我。

enter image description here

关于ios - 如何将最后一行添加到 UITableView 并平滑滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304274/

相关文章:

ios - 访问 token 持久性最佳实践 (iOS)

android - 代码可能在 iOS 上的 OpenGL ES 2.0 fragment 着色器中进行了优化

ios - 如何在 CAShapeLayer 的角上绘制 4 条虚线?

ios - 如何使用 Swift 从 iOS 上传图像到 Ruby/Rails 服务器

swift - 在同一扩展中调用时,集合扩展不可见?

iphone - 如何在编辑模式下向 UITableViewCell 添加 subview 并调整其大小?

ios - 使用 ReactiveCocoa 禁用 UIBarButtonItem

ios - 如何从 Swift 中的蓝牙特性中获取数据

uitableview - scrollToRowAtIndexPath 在 iOS 7 中停止工作 - ?UITableViewCellScrollView?更改 UITableViewCell 层次结构

ios - iPad:两个导航 Controller 同时管理两个 View Controller