1

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

在我的视图控制器中,我从这个 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];

}

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

4

1 回答 1

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];
于 2013-04-10T03:14:39.543 回答