【发布时间】:2018-06-16 03:28:27
【问题描述】:
我已经多次看到这个问题被问到,尽管我已经实施了社区提出的每个解决方案,但我仍然没有成功。我正在实施的是一个基本的公共聊天应用程序。我需要在 UITableView 中显示通过我的 API 收到的许多消息。为了有一种聊天的感觉,我通过将 UITableView 和 UITableViewCell 的 transform 属性更改为 CGAffineTransform(scaleX: 1, y: -1) 来颠倒我的 UITableView 和 UITableViewCell。相反,要将单元格添加到 UITableView,我首先通过 messages.insert(message, at: indexPath.row) 将传入消息添加到数组中,然后调用 insertRows(at: [indexPath], with: animation)(其中 indexPath 是以这种方式创建的 IndexPath(row: 0, section: 0))。当我在 UITableView 的底部时,一切都很好:新的单元格从下到上出现,伴随着流畅的动画。当我滚动到顶部几个像素时,问题就开始了。查看这些图片以更好地了解其中的区别。
我想要实现的是防止 UITableView 滚动,除非我在它的最底部,这样,如果用户滚动到顶部以阅读过去的消息,他可以这样做而不会引起任何麻烦UITableView 的移动。
我希望有人能指出我正确的方向。谢谢
编辑:如果有帮助,我正在使用自动 UITableViewCell 高度。
编辑:这是我当前的代码:
我正在使用通用包装器class ListView<Cell: UITableViewCell, Item>,此方法用于添加新项目:
func add(_ item: Item) {
items.insert(item, at: 0)
if contentOffset.y > -contentInset.top {
insertRows(at: [IndexPath(row: 0, section: 0)], with: .top)
} else {
reloadData()
}
}
我必须使用-contentInset.top 来检查我是否在滚动视图的最底部,因为我之前出于布局原因将 contentInset 设置为UIEdgeInsets(top: composeMessageView.frame.height - 4, left: 0, bottom: 4, right: 0)。同样,我将 estimatedRowHeight 设置为 44,将 rowHeight 设置为 UITableViewAutomaticDimension。
【问题讨论】:
-
你有没有试过在插入新行之前保存tableview的内容偏移量,插入后重新应用?
-
当然。它没有用。也许与一切都颠倒的事实有关?
标签: ios swift uitableview cocoa-touch uiscrollview