2017-04-17 93 views
0

無論刷新控件何時激活,我都試圖將數據加載到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 
+0

你在檢索數據後重新加載表嗎? –

+0

不要調用viewDidLoad,只有系統應該調用它。改爲調用tableView.reloadData。 – ObjectAlchemist

回答

0

其實你不修改,也不重新加載在刷新功能的任何方式表視圖和它的數據。您應該更新表格視圖的數據源,然後在更新後調用[tableView reloadData];

1

您需要重新載入您的數據。實施像這樣的Refreshcontroll:

- (void)viewDidLoad { 
    refreshControl = [[UIRefreshControl alloc] init]; 
    [refreshControl addTarget:self 
         action:@selector(update) 
      forControlEvents:UIControlEventValueChanged]; 

    [self.startScreenView addSubview:refreshControl]; 
} 

- (void) update { 
    [refreshControl beginRefreshing]; 
    [yourTableView reloadData]; 
    [refreshControl endRefreshing]; 
}