【问题标题】:Detecting Reachability检测可达性
【发布时间】:2013-01-23 06:28:56
【问题描述】:

我有从 Apple 采用的 Reachability 类。我的问题是在我的 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");
    

    }

【问题讨论】:

  • 你可以试试github.com/GlennChiu/GCNetworkReachability 会好一点。
  • 谢谢。但我仍然不知道如何在我的项目中实现它。我对目标 c 还很陌生。
  • 从网站下载,有一个按钮,上面写着 zip。然后将 2 个文件拖到应用程序项目中并确保正在编译 .m 文件(在构建阶段和编译源下),然后在要添加类型的类中 #import "" 并将其导入

标签: iphone ios objective-c xcode


【解决方案1】:

你可以这样编码,添加一个实例 var BOOL _isOffline 在课堂上和你的updateInterfaceWithReachability: 方法中

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

【讨论】:

  • 谢谢。但是什么 xxxNotReachable?这和 Not Reachable 一样吗?
  • @MrExperimental 是的,它是你的 NotReachable 枚举,因为不同版本的 Reachability 是 NotReachable 枚举name is different , so xxxNotReachable is your Not Reachable enums 的名称。
  • @MrExperimental ... _tableView 是您的 cells tableView , find out your tableViews 名称,并用您的名称更改 _tableView
  • 完成。但是,我意识到我需要输入一个 IP 地址,例如。 “www.google.com”检查我的应用程序是否连接到它。我怎么能比较它?如何实现?
  • @MrExperimental 您可以查看“自述文件”以了解如何通过以下方式使用可达性:github.com/tonymillion/Reachability
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多