【问题标题】:Reload TableView Data if user allow or decline get current location如果用户允许或拒绝获取当前位置,则重新加载 TableView 数据
【发布时间】:2010-10-02 18:39:34
【问题描述】:

我在应用中有CLLocationManager。所以我需要的是知道用户是否允许或拒绝当前位置的方法。根据用户回答 TableController 以特定方式在表中添加值。我尝试[sightsTableView reloadData]; in
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 但在我选择允许或拒绝后,tableview 不会重新加载。我检查了 Apple 的 CLLocation 参考,找不到有用的东西。 UPD:这里是代码示例。

 - (void)viewDidLoad {  
      super viewDidLoad];

      [[self locationManager] startUpdatingLocation]; 
      [tempTableView reloadData];
   }
 - (CLLocationManager *)locationManager {

 if (locationManager != nil) {
    return locationManager;
}

        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
        [locationManager setDelegate:self];



return locationManager;
}

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

          NSLog(@"User allow get location");
          self.locationError = NO;
          [tempTableView reloadData];

        }



- (void)locationManager:(CLLocationManager *)manager
         didFailWithError:(NSError *)error {
    NSLog(@"User declined get location");
    self.locationError = YES;
    [tempTableView reloadData];


    }

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

 .... some big code about a sells )...

    cell.textLabel.text = array.title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if(self.locationError == NO){
    cell.detailTextLabel.text = @"!";
            }
            return cell;
 }

更新: 实际上reload没有问题。我知道刷新表视图有问题。如果我开始滚动表格单元格显示新值。

第二次更新 现在桌子焕然一新。问题是我如何称呼UITableView。我使用了[tempTableView reloadData];,但它对我不起作用,所以我尝试使用这个[self.tableView reloadData];,现在它正在重新加载值并刷新它。

【问题讨论】:

    标签: iphone tableview cllocationmanager


    【解决方案1】:
    locmanager = [[CLLocationManager alloc] init];
    [locmanager setDelegate:self];
    [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
    
    if (!locmanager.locationServicesEnabled)
    {
        [contentView setText:@"Location Services Are Not Enabled"];
        return;
    }
    

    【讨论】:

    • 我实际上尝试过这种方法。但 TableView 加载速度比“位置启用程序”加载速度快。所以我需要在用户回答后重新加载 tebleview。
    • 你是在模拟器还是在真机上运行?
    【解决方案2】:

    您是否设置了断点并确认重新加载表视图数据的代码实际上正在运行?如果它没有运行,您确定您已将视图控制器设置为 CLLocationManager 的委托吗?

    要检测用户是否拒绝让您获取他们的当前位置,您可以实现 locationManager:didFailWithError: 并查看错误代码是否为 kCLErrorDenied。

    更新

    你的问题很大一部分是这些行

    BOOL locationError = NO;
    BOOL locationError = YES;
    

    这是创建一个只存在于该方法中的新变量,而不是设置存在于您的类中的 locationError 变量。您要么想要删除 BOOL 要么执行 self.locationError = NO;

    【讨论】:

    • 是的,我设置了一个断点,它的工作原理。我也使用didFailWithError 方法。看,我使用BOOL locationError = YES;,然后在didFailWithError 中重新加载[tempTableView reloadData];。接下来使用locationError 的应用是cellForRowAtIndexPathif(self.locationError == NO){ cell.detailTextLabel.text = @"1"; }。所以我认为在我用locationError == NO 重新加载表格视图之后它必须工作。
    • @sherilyn,你能发布你的 didFailWithError 和 cellForRowAtIndexPath 代码吗?你刚才说的一些东西我不太了解,所以如果我能看到一些代码,可能会更容易弄清楚。
    • 它不会改变任何东西。表格视图仍未重新加载(
    • 应用程序正在加载 TableView 然后弹出位置警报。我选择什么都没关系 - 表格视图仍然没有重新加载,但应该。
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多