Ios 重新加载数据是在旧数据的顶部进行堆叠
我知道在调用reloadData之前需要更改数据源中的数据。我的问题是,我不确定这是如何做到的,以及为什么我的getData方法不覆盖当前单元格。是否需要为此使用子视图?或者,在调用refresh创建一组新数据时,是否有方法重置单元格Ios 重新加载数据是在旧数据的顶部进行堆叠,ios,objective-c,xcode,Ios,Objective C,Xcode,我知道在调用reloadData之前需要更改数据源中的数据。我的问题是,我不确定这是如何做到的,以及为什么我的getData方法不覆盖当前单元格。是否需要为此使用子视图?或者,在调用refresh创建一组新数据时,是否有方法重置单元格 @property (nonatomic,strong) NSMutableArray *objectHolderArray; @end @implementation MartaViewController - (void)viewDidLoad {
@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];
}
}
因为您正在将对象添加到
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];
}
}
首先在viewDidLoad self.objectArray=[NSMutlabelArray alloc]init]中初始化数组,并在刷新表视图时使用[self.orderArray removeAllObject]从对象数组中删除所有对象,然后在新数组中复制新内容。首先在viewDidLoad self.objectArray=[NSMutlabelArray alloc]中初始化数组init]并在刷新表视图时,使用[self.orderArray removeAllObject]从对象数组中删除所有对象,然后在新数组中复制新内容。在将新数据集附加到objectHolderArray之前,应通过调用[objectHolderArray removeAllObjects]从objectHolderArray中删除所有旧对象方法。在将新数据集追加到objectHolderArray之前,应通过调用[objectHolderArray removeAllObjects]方法从objectHolderArray中删除所有旧对象。