ios - reloadData 堆叠在旧数据之上

标签 ios objective-c xcode

我明白我需要在调用reloadData之前更改数据源中的数据。我的问题是我不确定这是如何完成的以及为什么我的 getData 方法不会覆盖当前单元格。是否有必要为此使用 subview ?或者有没有办法在调用刷新以创建一组新数据时重置单元格?

@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

最佳答案

因为您要将对象添加到 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];
        }
    }

关于ios - reloadData 堆叠在旧数据之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463704/

相关文章:

xcode - 无法使用 iOS OpenCV 模块从 Titanium Appcelerator 编译应用程序

ios - iOS的Firefox上是否可以使用Service Worker?

android - 适用于 IOS 或 Android 的 Python

ios - Unity 游戏在 iOS 上加载时崩溃

objective-c - NSString 中子字符串的出现次数?

objective-c - Xcode:为什么我不能在一个项目中有重复的 ivars?出现重复符号错误

objective-c - NSFetchedResultsController 不对加密属性进行排序

iOS:在没有 UITabBarController 的情况下从用户界面实现 UITabBar

ios - 需要了解iOS In App购买收据

ios - 如何降低 iCarousel View 的速度?