我在我的应用程序中使用拉动来刷新 Collection View ,但应用程序因错误而崩溃。
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds for empty array'
我正在使用异步队列从服务器获取数据。
以下是我从服务器获取数据并成功更新用户界面的代码:
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(startRefresh:)
forControlEvents:UIControlEventValueChanged];
- (void)startRefresh:(UIRefreshControl *)refreshcontrol
{
dispatch_queue_t fetchQueue = dispatch_queue_create("fetch Queue", NULL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//webservice call which on success post notification
});
}
- (void)onSuccess:(NSNotification *)success { //notification after success of webservice
[storyCollectionView setHidden:NO];
NSLog(@"fun:%s", __func__);
dispatch_async(dispatch_get_main_queue(), ^{//in the main queue i'm ending refresing
if ([refreshControl isRefreshing]) {
[refreshControl endRefreshing];
}
[CollectionView reloadData];// reloading collection view
}
);
}
最佳答案
我认为您应该在调用refreshcontrol.endrefreshing 后重新加载 Collection View 或 TableView 。 喜欢:-
@objc func refresh(sender:AnyObject) {
// Code to refresh table view
//Do all other API Calling stuff here
refreshControl.endRefreshing()
self.tableviewList?.reloadData()
}
它将完美运行!! :)
关于ios - 拉动刷新崩溃 ios 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22631681/