我有一個使用標準的選項卡欄應用程序項目模板(UITabBarController)的iPhone應用程序(在XCode 4中)。如何刷新表格數據當用戶返回到UITabBarController選項卡
我的第一個視圖有一個UITableView從JSON提要獲取數據。在此處,該數據的控制器負載:
#import "FirstViewController.h"
#import "SBJson.h"
#import "WheelRecord.h"
#import "WheelTableCell.h"
#import "UIImageView+WebCache.h"
@implementation FirstViewController
@synthesize wheels, wheelsTable;
- (NSMutableArray *) wheels {
if (!wheels) {
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wheelspotting.com/recent.json"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *wheelData = [parser objectWithString:json_string error:nil];
self.wheels = [NSMutableArray array];
for (NSDictionary *wheel in wheelData)
{
WheelRecord *currentWheel = [[[WheelRecord alloc] init] autorelease];
currentWheel.title = [wheel objectForKey:@"title"];
currentWheel.smallImage = [wheel objectForKey:@"small_image"];
currentWheel.mediumImage = [wheel objectForKey:@"medium_image"];
currentWheel.largeImage = [wheel objectForKey:@"large_image"];
[self.wheels addObject:currentWheel];
}
}
return wheels;
}
我想用戶選擇不同的選項卡的任何時間重新加載數據,然後返回到第一個標籤。
我試過在viewDidLoad和viewWillAppear中設置輪子爲零,但是當我切換標籤並回到第一個標籤時,它永遠不會更新(我對要測試的數據進行更改)。
是的,我沒有調用reloadData ...我認爲將輪子設置爲零會迫使它重新獲取數據,因爲桌子使用輪子。但我想它保持數據直到reloadData被調用。我不完全理解它,但它起作用。謝謝。 – Callmeed
當表格視圖首次被初始化時,它會詢問(通過它的數據源)方法來得到部分和單元的數量。然後,每當一個單元格變得可見時(即通過滾動),它會詢問該單元格的內容,因此理論上如果您的數據被更新並且表格沒有重新加載,那麼您可能會看到一些數據通過滾動視圖,但也可能導致數組超出界限等(因爲總數尚未更新)。每當數據依賴於更改時,總是最好爲視圖重新加載數據。 –