ios - 拉动刷新崩溃 ios 应用程序

标签 ios objective-c uicollectionview pull-to-refresh

我在我的应用程序中使用拉动来刷新 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/

相关文章:

ios - 自定义 UICollectionViewCell 不显示 UIImage 和崩溃

iphone - 如何在 iOS 中创建类似谷歌报亭的设计

ios - 使用 UICollectionViewCell 上的按钮显示数组中的数据

ios - 重新加载 UITableView 数据而不重新定位 UITableViewCells

javascript - window.print()和window.close()函数Safari IOS在不等待打印预览的情况下关闭窗口

ios - 设置 UIButton 的框架但保持图像大小相同

objective-c - NSScrollView 内的 NSImageView 平铺在缩小时绘制不需要的边框

ios - Xcode Storyboard显示与模拟器不同的设计?

ios - 如何在 UILabel 中嵌入小图标

iphone - 在 Objective-c 中编码二维数组