[英]Swift 1.2: UITableView.reloadData() Isn't called quick enough. How can I make sure the data is loaded before I scroll?
I want to make sure my tableView data is fully loaded before I tell it to scroll to the last cell at the bottom. 我要确保我的tableView数据已完全加载,然后再告诉它滚动到底部的最后一个单元格。 Here is the code I have:
这是我的代码:
self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(index: (self.chatMessages.count - 1)), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
Which doesn't work, and only alerts the UITableView instead of calling the method. 这是行不通的,只会警告UITableView而不是调用方法。 I've tried to call reloadData() from the main thread and that still doesn't work.
我试图从主线程调用reloadData(),但这仍然行不通。
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
self.tableView.scrollToRowAtIndexPath(NSIndexPath(index: (self.chatMessages.count - 1)), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
How can I make this work without a SIGBART Error? 如何在没有SIGBART错误的情况下进行这项工作?
Here's my stack trace: 这是我的堆栈跟踪:
2015-08-11 16:31:17.026 SuperCommunication[13769:441407] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: section (1) beyond bounds (1).'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105316c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106e81bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000105316b9d +[NSException raise:format:] + 205
3 UIKit 0x0000000105ca447d -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 429
4 UIKit 0x0000000105ca5338 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 33
5 SuperCommunication 0x0000000103f023f4 _TFC18SuperCommunication14ViewController30loadMessagesForSelectedChannelfS0_FT_T_ + 852
6 SuperCommunication 0x0000000103f0006d _TFC18SuperCommunication14ViewController9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 973
7 SuperCommunication 0x0000000103f000ef _TToFC18SuperCommunication14ViewController9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 79
8 UIKit 0x0000000105ca6d89 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1293
9 UIKit 0x0000000105ca6eca -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
10 UIKit 0x0000000105bd95ec _applyBlockToCFArrayCopiedToStack + 314
11 UIKit 0x0000000105bd9466 _afterCACommitHandler + 533
12 CoreFoundation 0x0000000105249ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
13 CoreFoundation 0x0000000105249c00 __CFRunLoopDoObservers + 368
14 CoreFoundation 0x000000010523fa33 __CFRunLoopRun + 1123
15 CoreFoundation 0x000000010523f366 CFRunLoopRunSpecific + 470
16 GraphicsServices 0x0000000109388a3e GSEventRunModal + 161
17 UIKit 0x0000000105bb58c0 UIApplicationMain + 1282
18 SuperCommunication 0x0000000103f0dbf7 main + 135
19 libdyld.dylib 0x00000001075b7145 start + 1
20 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Your problem is in wrong NSIndexPath
object initializer. 您的问题出在错误的
NSIndexPath
对象初始化程序中。 You need to use NSIndexPath(forRow: yourRow, inSection: yourSection)
: 您需要使用
NSIndexPath(forRow: yourRow, inSection: yourSection)
:
self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.chatMessages.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
Update: 更新:
Maybe performing scroll after some delay will help you: 也许经过一些延迟后执行滚动会帮助您:
self.tableView.reloadData()
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.chatMessages.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}
One possible solution would be 一种可能的解决方案是
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
if indexPath.row == self.chatMessages.count - 1 {
//perform scroll
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.