2013-04-10 48 views
1

我想在重新加载数据之前删除表格视图中的所有行,但似乎无法使其工作。在重新加载之前删除表格行

在我的viewcontroller中,我从这个AFNetworking请求中获得我的数组。

- (void)viewDidLoad 
    [[LocationApiClient sharedInstance] getPath:@"locations.json" 
            parameters:nil 
             success: 
       ^(AFHTTPRequestOperation *operation, id response) { 
        NSLog(@"Response: %@", response); 
        NSMutableArray *location_results = [NSMutableArray array]; 
        for (id locationDictionary in response) { 
         Location *location = [[Location alloc] initWithDictionary:locationDictionary]; 
         [location_results addObject:location]; 
        } 
        self.location_results = location_results; 
        [self.tableView reloadData]; 
        } 
            failure: 
       ^(AFHTTPRequestOperation *operation, NSError *error) { 
        NSLog(@"Error fetching locations!"); 
        NSLog(@"%@", error); 
       }]; 
} 

而且我想删除的数据,然后通过这个按钮

- (IBAction)locationPressed:(id)sender { 

    [[Location sharedSingleton].locationManager startUpdatingLocation]; 
    [self viewDidLoad]; 
    NSMutableArray *location_results = [NSMutableArray array]; 
    [location_results removeAllObjects]; 
    [self.tableView reloadData]; 

} 

重新加载它,但它不删除行。我看到重载发生在已经存在的行的顶部。有什么建议么?

回答

2

千万不要手动拨打viewDidLoad

创建像

- (void)reloadDataWithCompletion:(void(^)(NSArray *locations))completion 
         failure:(void(^)(NSError *error))failure { 
    [[LocationApiClient sharedInstance] getPath:@"locations.json" 
            parameters:nil 
             success: 
      ^(AFHTTPRequestOperation *operation, id response) { 
       NSLog(@"Response: %@", response); 
       NSMutableArray *location_results = [NSMutableArray array]; 
       for (id locationDictionary in response) { 
        Location *location = [[Location alloc] initWithDictionary:locationDictionary]; 
        [location_results addObject:location]; 
       } 
        if(completion) 
         completion(location_results); 
       } 
           failure: 
      ^(AFHTTPRequestOperation *operation, NSError *error) { 
       if(failure) 
        failure(error); 
      }]; 
} 

的方法,并调用它时,你需要重新加载数据

- (void)viewDidLoad { 
    [super viewDidLoad]; // Don't forget the call to super! 

    [self reloadDataWithCompletion:^(NSArray *locations) { 
     self.location_results = locations; 
     [self.tableView reloadData]; 
    } failure:^(NSError *error) { 
     NSLog(@"Error fetching locations!"); 
     NSLog(@"%@", error); 
    }]; 
} 

- (IBAction)locationPressed:(id)sender { 
    [[Location sharedSingleton].locationManager startUpdatingLocation]; 
    [self reloadDataWithCompletion:^(NSArray *locations) { 
     self.location_results = locations; 
     [self.tableView reloadData]; 
    } failure:^(NSError *error) { 
     NSLog(@"Error fetching locations!"); 
     NSLog(@"%@", error); 
    }]; 
} 

为了实现重载,你可以做表的图形效果(假设你只有一个部分),替代品

[self.tableView reloadData]; 

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 
+0

Thanks @Gabriele,这是非常有帮助的。我应该在哪里调用removeAllObjects?在重新加载之前,实施建议仍然不会清除表格视图。 – jacobt 2013-04-10 04:18:09

+0

@jacobt这取决于你想达到什么目的?你想要消失的图形效果吗?或者干脆重新加载整个表? – 2013-04-10 04:19:17

+0

我希望按下按钮后消失的图形效果,然后用新数据重新加载表格单元格。可能吗?谢谢你的帮助! – jacobt 2013-04-10 04:25:40

相关问题