2017-04-18 44 views
0

據我所知,我需要在調用reloadData之前更改數據源中的數據。我的問題是我不確定這是如何完成的,爲什麼我的getData方法不覆蓋當前單元格。是否有必要使用子視圖呢?或者當調用刷新以創建一組新數據時,是否有辦法重置單元格?reloadData正在堆疊舊數據的ontop

@property (nonatomic,strong) NSMutableArray *objectHolderArray; 
@end 



@implementation MartaViewController 
- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    [self getData]; 

    //to add the UIRefreshControl to UIView 
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; 
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Please Wait..."]; 
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged]; 

} 


- (void)getData 
    { 
     NSURL *blogURL = [NSURL URLWithString:JSON_URL]; 
     NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 
     NSError *error = nil; 
     NSDictionary *dataDictionary = [NSJSONSerialization 
             JSONObjectWithData:jsonData options:0 error:&error]; 
     for (NSDictionary *bpDictionary in dataDictionary) { 
      Object *currenHotel = [[Object alloc]Station:[bpDictionary objectForKey:@"station"] Status:[bpDictionary objectForKey:@"status"]]; 
      [self.objectHolderArray addObject:currenHotel]; 
     } 
    } 


- (IBAction)refresh:(UIRefreshControl *)sender { 

    [self getData]; 
    [self.tableView reloadData]; 
    [sender endRefreshing]; 

} 



#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: 
(NSInteger)section 
{ 
    return [self.objectHolderArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    MartaViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                 forIndexPath:indexPath]; 
    Object *currentHotel = [self.objectHolderArray 
           objectAtIndex:indexPath.row]; 


    cell.lblStation.text = currentHotel.station; 
    cell.lblStatus.text = currentHotel.status; 


    return cell; 
} 
-(NSMutableArray *)objectHolderArray{ 
    if(!_objectHolderArray) _objectHolderArray = [[NSMutableArray alloc]init]; 
    return _objectHolderArray; 
} 


@end 
+0

在將新數據集追加到objectHolderArray之前,應通過調用[objectHolderArray removeAllObjects]方法從objectHolderArray中刪除所有舊對象。 –

回答

2

因爲您要添加的對象,而不是self.objectHolderArraygetData方法覆蓋。嘗試這種

- (void)getData 
    { 
     NSURL *blogURL = [NSURL URLWithString:JSON_URL]; 
     NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 
     NSError *error = nil; 
     NSDictionary *dataDictionary = [NSJSONSerialization 
             JSONObjectWithData:jsonData options:0 error:&error]; 
     [self.objectHolderArray removeAllObjects]; 
     for (NSDictionary *bpDictionary in dataDictionary) { 
      Object *currenHotel = [[Object alloc]Station:[bpDictionary objectForKey:@"station"] Status:[bpDictionary objectForKey:@"status"]]; 
      [self.objectHolderArray addObject:currenHotel]; 
     } 
    } 
0

首先初始化viewDidLoad中self.objectArray = [NSMutlabelArray的alloc]初始化數組],當你刷新表視圖使用從對象數組中刪除所有對象[self.orderArray removeAllObject]複製新的內容在新陣列中。