2013-04-10 82 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

相關問題