簡體   English   中英

檢測可達性

[英]Detecting Reachability

我有從Apple采納的可達性課程。 我的問題是在ListViewController中而不是在Apple中顯示的ReachabilityAppDelegate中實現我的可達性檢測。 我的問題:

  1. 我想在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath和可達性檢測中鏈接調用方法

  2. 我試圖禁用我的單元格(如果他們檢測到未連接),並啟用該單元格(如果它已連接)
    已連接

這是在viewDidLoad中編碼的:

   [[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object: nil];

可達性更改如下:

-(void) reachabilityChanged: (NSNotification* )note{
  Reachability* curReach = [note object];
  NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
  [self updateInterfaceWithReachability: curReach];
}

我如何在中實現禁用UITableViewCells

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  • 請注意,我已經在上面的方法中對此進行了編碼:

     NSInteger row = [indexPath row]; NSString *contentForThisRow = nil; static NSString *MyIdentifier = @"MyIdentifier"; if (tableView == [[self searchDisplayController] searchResultsTableView]) { // Sort search results in alphabetical order NSArray *sorted = [searchResults sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; contentForThisRow = [sorted objectAtIndex:row]; }else { contentForThisRow = [nameArray objectAtIndex:row]; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]autorelease]; } // Set Device names into Cells cell.textLabel.text = contentForThisRow; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; NSLog(@"Load cell done"); 

    }

您可以這樣編寫代碼,在類和您的updateInterfaceWithReachability:方法中添加實例var BOOL _isOffline

- (void)updateInterfaceWithReachability:(Reachability* )curReach
{
    if(curReach == XXXXNotReachable)
    {
        //your code
        _isOffline = YES;
    }
    else
    {
        _isOffline = NO;
    }
    [_tableView reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

您應該添加代碼來處理單元格,可能是

if(_isOffline)
{
    cell.userInteractionEnabled = NO;
}
else
{
    cell.userInteractionEnabled = YES;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM