無論刷新控件何時激活,我都試圖將數據加載到UITableViewCell
中。我的代碼成功地檢索了數據,我可以使用NSLog
來驗證,但ViewCell在刷新控制結束時不會使用新數據進行更新。任何人都可以將我指向正確的方向嗎?如何使用刷新控件刷新TableViewCell數據
下面是MyViewController.m文件:
#define JSON_URL @"https://website.com"
#import "MyViewController.h"
#import "Object.h"
#import "MyViewCell.h"
@interface MyViewController()
@property (nonatomic,strong) NSMutableArray *objectHolderArray;
@end
@implementation MyViewController
- (void)viewDidLoad
{
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];
}
[super viewDidLoad];
//to add the UIRefreshControl to UIView
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Please Wait..."]; //to give the attributedTitle
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
}
- (IBAction)refresh:(UIRefreshControl *)sender {
[self viewDidLoad];
[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
你在檢索數據後重新加載表嗎? –
不要調用viewDidLoad,只有系統應該調用它。改爲調用tableView.reloadData。 – ObjectAlchemist