[self.tableVIEw beginUpdates];[messages addobject:msg];[self.tableVIEw insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:messages.count - 1 inSection:1]] withRowAnimation:UItableVIEwRowAnimationBottom];[self.tableVIEw endUpdates];[self.tableVIEw scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:messages.count - 1 inSection:1] atScrollposition:UItableVIEwScrollpositionBottom animated:YES];
但是,当我添加新消息(发送和接收,结果在两者中是一样的)时,我的表视图“跳跃”:
为什么我得到这个奇怪的“跳”?
解决方法 好的,我想出来了就像你说的,这个问题与自动调整细胞有关.我使用两个技巧来使事情工作(我的代码在Swift中,但应该很容易翻译成ObjC):1)等待桌面动画完成,然后再采取进一步措施.这可以通过将代码更新到CATransaction.begin()和CATransaction.commit()之间的块内的代码来完成.我在CATransaction中设置完成块 – 该代码将在动画完成后运行.
2)强制表视图在滚动到底部之前呈现单元格.我通过增加表的contentOffset少量来做到这一点.这将导致新插入的单元格出现,并计算其高度.一旦滚动完成(我等待它完成使用上面的方法(1)),我终于调用tableVIEw.scrollToRowAtIndexPath.
以下是代码:
overrIDe func vIEwDIDLoad(){ super.vIEwDIDLoad() // Use auto-sizing for rows tableVIEw.estimatedRowHeight = 40 tableVIEw.rowHeight = UItableVIEwautomaticDimension tableVIEw.dataSource = self}func chatManager(chatManager: ChatManager,dIDAddMessage message: ChatMessage){ messages.append(message) let indexPathToInsert = NSIndexPath(forRow: messages.count-1,inSection: 0) CATransaction.begin() CATransaction.setCompletionBlock({ () -> VoID in // This block runs after the animations between CATransaction.begin // and CATransaction.commit are finished. self.scrollTolastMessage() }) tableVIEw.beginUpdates() tableVIEw.insertRowsAtIndexPaths([indexPathToInsert],withRowAnimation: .Bottom) tableVIEw.endUpdates() CATransaction.commit()}func scrollTolastMessage(){ let bottomrow = tableVIEw.numberOfRowsInSection(0) - 1 let bottomMessageIndex = NSIndexPath(forRow: bottomrow,inSection: 0) guard messages.count > 0 else { return } CATransaction.begin() CATransaction.setCompletionBlock({ () -> VoID in // Now we can scroll to the last row! self.tableVIEw.scrollToRowAtIndexPath(bottomMessageIndex,atScrollposition: .Bottom,animated: true) }) // scroll down by 1 point: this causes the newly added cell to be dequeued and rendered. let contentOffset = tableVIEw.contentOffset.y let newContentOffset = CGPointMake(0,contentOffset + 1) tableVIEw.setContentOffset(newContentOffset,animated: true) CATransaction.commit()}总结
以上是内存溢出为你收集整理的ios – scrollToRowAtIndexPath:atScrollPosition导致表视图“跳转”全部内容,希望文章能够帮你解决ios – scrollToRowAtIndexPath:atScrollPosition导致表视图“跳转”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)