2014-03-28 192 views
0

在此先感謝。UITableView在互聯網連接丟失後不顯示

我正在使用Apple的Reachability Class文件來獲取Internet狀態。當互聯網處於活動狀態時,我將調用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]; 

這是我在showData方法寫入

- (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; 
    } 
} 

}

方法我只是重新加載表視圖[cardTableView reloadData];

+0

這是什麼應該做的:'爲(EGORefreshTableHeaderView *在[cardsTableView子視圖]視圖){ [查看removeFromSuperview] }'? –

+0

基本上,我刪除拉來刷新視圖,我已經添加了UITableViewHeaderView @ alex-i – Vishnu

回答

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

上述代碼去除cardsTableView所有子視圖,而不僅僅是是EGORefreshTableHeaderView類型的子視圖。

另一種方式來寫,這將是:

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

你只是只有通過實際上EGORefreshTableHeaderView對象告訴for循環處理的對象爲EGORefreshTableHeaderView實例,而不是循環。

相反,你想是這樣的:

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

甚至更​​好,如果你有一個部門首長拉刷新一個變量,你可以跳過循環都在一起。我的猜測是_refreshHeaderView是你的情況下的變量,如果是這樣,你可以安全地刪除循環。

請注意,您在這裏有另一個bug:

_refreshHeaderView = nil; 
[_refreshHeaderView removeFromSuperview]; 

這就像調用[nil removeFromSuperview]; - 它什麼都不做。你需要扭轉的順序爲:

[_refreshHeaderView removeFromSuperview]; 
_refreshHeaderView = nil; 
相關問題