iphone - MBProgress HUD 在 tableView 上的 reloadData 上崩溃应用程序

标签 iphone objective-c ios uitableview mbprogresshud

当我从网络服务下载数据时,我正在使用这段代码在我的一个 View 的顶部显示 MBProgressHUD,唯一的问题是偶尔这段代码会导致应用程序挂起,同时什么都不做HUD 显示“正在下载”并且屏幕被锁定。另外,如果我在按下刷新按钮(刷新按钮执行下载)时向用户显示类似键盘的东西,那么应用程序就会崩溃:

 [self.tableView reloadData];

我的代码:

//Checks for network connection then displays HUD while executing pullAndDisplayData method
- (IBAction) update {
    UIAlertView *errorView;

    if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
        errorView = [[UIAlertView alloc] 
                     initWithTitle: @"Network Error" 
                     message: @"No Network connection availible!" 
                     delegate: self 
                     cancelButtonTitle: @"OK" otherButtonTitles: nil]; 
        [errorView show];
    }
    else
    {
        HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
        [self.navigationController.view addSubview:HUD];

        HUD.delegate = self;
        HUD.labelText = @"Downloading";
        HUD.minSize = CGSizeMake(135.f, 135.f);

        [HUD showWhileExecuting:@selector(pullAndDisplayData) onTarget:self withObject:nil animated:YES];
    }
}

//Downloads this users data from the web-service
- (void) pullAndDisplayData{
    // Indeterminate mode
    ExpensesDataDownloader *downloader = [[ExpensesDataDownloader alloc] init];
    [downloader pullAndDisplayData];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([[defaults objectForKey:@"canExportCSVServer"] isEqualToString:@"1"])
    {

    }

    [self.tableView reloadData];

    // Switch to determinate mode
    HUD.mode = MBProgressHUDModeDeterminate;
    HUD.labelText = @"Updating";
    float progress = 0.0f;
    while (progress < 1.0f)
    {
        progress += 0.01f;
        HUD.progress = progress;
        usleep(15000);
    }
    // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = @"Completed";
    sleep(2);
}

如有任何帮助,我们将不胜感激。

jack

最佳答案

pullAndDisplayData 方法在单独的线程上运行。这样 MBProgressHUD 就可以使用 UI 线程来显示自己。您应该始终从主 (UI) 线程更新您的 UI。使用 performSelectorOnMainThread: 方法调用 [self.tableView reloadData]; 和其他 UI 内容。我假设 [downloader pullAndDisplayData]; 是同步调用。

关于iphone - MBProgress HUD 在 tableView 上的 reloadData 上崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11411680/

相关文章:

ios - 在 iPhone 上仅使用自定义 viewcontroller 类创建 popoverview

iphone - 在cocos2d中绘制连续的渐变线

ios - PresentViewController dismiss 不会返回到之前的 viewController

objective-c - 使用 LAPACK 求解 Ax=B,其中 x >= 0

iphone - 使用 native Trigger.IO API 将 UIImage 传递给 JS

ios - iPhone 5, 6, 6 plus ui设计问题

ios - NSRegularExpression,指定区分大小写匹配?

ios - Swift:使用本地化的百万和十亿文本格式化货币

objective-c - NSString unicode 下标

ios - 如何设置 contentScaleFactor iOS