Swift 1.2:UITableView.reloadData()没有足够快速调用。在滚动之前如何确保数据已加载?

时间:2015-08-11 23:21:13

标签: ios swift uitableview

在我告诉它滚动到底部的最后一个单元格之前,我想确保我的tableView数据已完全加载。这是我的代码:

self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(index: (self.chatMessages.count - 1)), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

哪个不起作用,只提醒UITableView而不是调用方法。我试图从主线程调用reloadData(),但仍然无法正常工作。

dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}

self.tableView.scrollToRowAtIndexPath(NSIndexPath(index: (self.chatMessages.count - 1)), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

如何在没有SIGBART错误的情况下完成此工作?

这是我的堆栈跟踪:

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) 

2 个答案:

答案 0 :(得分:2)

您的问题出现在错误的NSIndexPath对象初始值设定项中。 您需要使用NSIndexPath(forRow: yourRow, inSection: yourSection)

self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.chatMessages.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

更新

在延迟一段时间后执行滚动可能会对您有所帮助:

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)
}

答案 1 :(得分:1)

一种可能的解决方案是

func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
    if indexPath.row == self.chatMessages.count - 1 {
       //perform scroll
    }
}