互联网连接丢失后,UITableView无法显示

时间:2014-03-28 07:06:49

标签: iphone uitableview reachability

先谢谢。

我正在使用Apple的Reachability Class文件来获取互联网状态。当互联网处于活动状态时,我正在调用webservie并将响应数据存储在sqlite3数据库中并显示在UITableView中。当互联网丢失时,我正在从数据库中检索数据并在UITableView中显示它,然后UITableView没有显示数据,但总代码工作正常。我不知道为什么会这样,请帮助我。这是我的代码

为了检查互联网,我在viewWillAppear中编写了以下代码:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    hostReach = [Reachability reachabilityWithHostName:@"www.kluebook.com"];
    [hostReach startNotifier];

这是我写的方法

 - (void)checkNetworkStatus:(NSNotification *)notice {
NetworkStatus internetStatus = [internetReach currentReachabilityStatus];
switch (internetStatus) {
    case NotReachable:
        self.internetActive = NO;
        break;
    case ReachableViaWiFi:
        self.internetActive = YES;
        break;
    case ReachableViaWWAN:
        self.internetActive = YES;
        break;
}
NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
switch (hostStatus) {
    case NotReachable:
        self.hostActive = NO;
        break;

    case ReachableViaWiFi:
        self.hostActive = YES;
        break;
    case ReachableViaWWAN:
        self.hostActive = YES;
        break;
}
if (internetActive && hostActive) {
    for (UIButton *subView in [self.view subviews]) {
        if ([subView isKindOfClass:[UIButton class]] && subView.tag == 1001) {
            [subView removeFromSuperview];
        }
    }
    [self showSpinner];
    if (viewTag == 2009) {
        [self performSelectorInBackground:@selector(stopSpinnig) withObject:nil];
        NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:[NSNumber numberWithInteger:0] forKey:@"viewTag"];
        [ud synchronize];
    }
    else {
        [self getLoadingData];
    }
    createCardBtn.enabled = YES;
}
if (self.internetActive == NO) {
    createCardBtn.enabled = NO;
    for (EGORefreshTableHeaderView *view in [cardsTableView subviews]) {
        [view removeFromSuperview];
    }
    _refreshHeaderView = nil;
    [_refreshHeaderView removeFromSuperview];

    networkStatusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    networkStatusBtn.userInteractionEnabled = NO;
    networkStatusBtn.frame = CGRectMake(0, 66, 320, 40);
    [networkStatusBtn setImage:[UIImage imageNamed:@"warning.png"] forState:UIControlStateNormal];
    [networkStatusBtn setTitle:@"No Internet Connection" forState:UIControlStateNormal];
    networkStatusBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 65, 5, 225);
    networkStatusBtn.titleEdgeInsets = UIEdgeInsetsMake(10, 55, 10, 65);
    networkStatusBtn.backgroundColor = [UIColor blackColor];
    networkStatusBtn.titleLabel.textColor = [UIColor whiteColor];
    networkStatusBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0];
    networkStatusBtn.tag = 1001;
    [self.view addSubview:networkStatusBtn];
    resultArray = [sql getAllCardsBasedOnType:701];
    if (resultArray.count == 0) {
        filterBtn.enabled = NO;
        cardsTableView.backgroundView = bgImageView;
        [cardsTableView setScrollEnabled:NO];
    }
    else {
        filterBtn.enabled = YES;
        [cardsTableView setScrollEnabled:YES];
        //cardsTableView.backgroundColor = [UIColor clearColor];
        [self performSelectorOnMainThread:@selector(showTheData) withObject:nil waitUntilDone:YES];
    }
    if (isLoaderViewShowing == YES) {
        [loaderView removeFromSuperview];
        isLoaderViewShowing = NO;
    }
}

}

showData方法中的

我只是重新加载表视图[cardTableView reloadData];

1 个答案:

答案 0 :(得分:1)

for (EGORefreshTableHeaderView *view in [cardsTableView subviews]) { 
  [view removeFromSuperview]; 
}

上面的代码会删除cardsTableView的所有子视图,而不仅仅是EGORefreshTableHeaderView类型的子视图。

另一种写作方式是:

for (id objInSubviews in [cardsTableView subviews]) { 
  EGORefreshTableHeaderView *view = (EGORefreshTableHeaderView *)objInSubviews;
  [view removeFromSuperview]; 
}

您只是告诉for循环将对象视为EGORefreshTableHeaderView个实例,而不是仅通过实际上EGORefreshTableHeaderView的对象循环。

相反,你需要这样的东西:

for (id objInSubviews in [cardsTableView subviews]) { 
  if([objInSubviews isKindOfClass:[EGORefreshTableHeaderView class]]){
    EGORefreshTableHeaderView *view = (EGORefreshTableHeaderView *)objInSubviews;
    [view removeFromSuperview]; 
  }
}

或者甚至更好,如果你有一个变量来提示刷新,你可以一起跳过循环。我的猜测是_refreshHeaderView在你的情况下是那个变量,如果是这样,你可以安全地删除循环。

请注意,您还有其他错误:

_refreshHeaderView = nil;
[_refreshHeaderView removeFromSuperview];

这就像调用[nil removeFromSuperview];一样 - 它什么都不做。您需要撤销订单:

[_refreshHeaderView removeFromSuperview];
_refreshHeaderView = nil;