reloadData正在堆叠旧数据

时间:2017-04-18 04:43:43

标签: ios objective-c xcode

我知道在调用reloadData之前我需要更改数据源中的数据。我的问题是,我不确定这是怎么做的以及为什么我的getData方法不会覆盖当前的单元格。是否有必要使用子视图?或者有没有办法在调用刷新时重置单元格以创建一组新数据?

@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

2 个答案:

答案 0 :(得分:3)

因为您要将对象添加到self.objectHolderArray而不是在getData方法中覆盖。试试这个

- (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];
        }
    }

答案 1 :(得分:0)

首先在viewDidLoad self.objectArray = [NSMutlabelArray alloc] init]中初始化数组,当你刷新表视图时,使用[self.orderArray removeAllObject]从对象数组中删除所有对象,复制新数组中的新内容。