MBProgressHUD 条件执行

发布于 2024-12-25 20:17:44 字数 1438 浏览 1 评论 0原文

我有一个表视图,其内容是使用 json 数组生成的。它还采用位置服务,以便当用户位置发生变化时,表会重新加载以反映表数据相对于用户位置的变化。

我的问题是我在视图加载开始时显示活动指示器,但每次位置更新和表重新加载时,都会显示该指示器。这是在非常短的时间内完成的,导致应用程序崩溃。

是否可以在以下代码中添加一个条件,以便如果它不是初始加载但由于位置更改而导致重新加载,则它不会显示 MBProgressHUD?

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];
    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
    return [rows count];
}

这是执行重新加载的地方:

    - (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

    NSString *lat = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];

    NSString *longt = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];

    //CGFloat Alat = [lat floatValue];
    //CGFloat Alon = [longt floatValue];
    self.aalat = lat;
    self.aalon = longt;
    //        NSLog(@"anlat: %@", aalat);
    //        NSLog(@"anlong: %@", aalon);
    [[NSUserDefaults standardUserDefaults] setObject:aalat forKey:@"latitude"];
    [[NSUserDefaults standardUserDefaults] setObject:aalon forKey:@"longitude"]; 
    [tableview reloadData];
}

I have a tableview whose contents are generated with json array. It also employes location services so as users' location changes, the table is reloaded to reflect the changes of the table data relative to users' location.

My problem is I show the activity indicator in the beginning of the view load, but each time location updates and table reloads, the indicator is shown. This is done in very short intervals, causing the application to crash.

Is it possible to put a condition to following code so that it wonT show MBProgressHUD if it is not the initial load but it is there reload caused by the location changes?

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];
    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
    return [rows count];
}

This is where the reload is performed:

    - (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

    NSString *lat = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];

    NSString *longt = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];

    //CGFloat Alat = [lat floatValue];
    //CGFloat Alon = [longt floatValue];
    self.aalat = lat;
    self.aalon = longt;
    //        NSLog(@"anlat: %@", aalat);
    //        NSLog(@"anlong: %@", aalon);
    [[NSUserDefaults standardUserDefaults] setObject:aalat forKey:@"latitude"];
    [[NSUserDefaults standardUserDefaults] setObject:aalon forKey:@"longitude"]; 
    [tableview reloadData];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2025-01-01 20:17:44

我会从 viewDidLoad 或 viewWillAppear 方法触发 HUD 来最初加载内容。从桌面视图射击会给你带来很多问题。当用户滚动时,tableview 方法会被重复触发,因此您总是会遇到这样的问题。

从您的位置更新中触发一个方法来显示 HUD。无论如何,它必须在主线程上运行才能使动画正常工作。我会从您的新方法中调用重新加载表,以避免表视图出现任何问题。

I would trigger the HUD from your viewDidLoad or viewWillAppear methods to load things up initially. Firing from the tableview is going to give you lots of problems. The tableview methods are fired repeatedly when the user scrolls, so you will always have issues with that going on.

Fire a method from your location update to display the HUD. It has to run on the main thread anyway for the animations to work correctly. I would call the reload table from your new method to avoid any issues with your tableview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文