如何在用户返回UITabBarController选项卡时刷新表数据

时间:2011-07-19 19:15:18

标签: iphone json uitableview uitabbarcontroller

我有一个iPhone应用程序(在XCode 4中),它使用标准的Tab Bar Application项目模板(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中将轮子设置为nil但是当我切换选项卡并返回第一个选项卡时,它永远不会更新(我对要测试的数据进行更改)。

2 个答案:

答案 0 :(得分:2)

我不相信会再次调用ViewDidLoad,因为所有视图控制器都会一直存在。我本以为应该调用viewWillAppear。

愚蠢的问题,但你确定数据没有更新?也许只是你的表视图没有用新数据更新?您是在viewWillAppear或viewDidAppear方法中的某处调用[yourTableView reloadData]?或者,在if代码结束之前弹出[yourTableView reloadData],例如

       [self.wheels addObject:currentWheel];
       [yourTableView reloadData]; // where yourTableView is the pointer to your table view
    }

标签栏控制器可能很难处理,我知道我个人花了很多时间咒骂他们。有一件事要尝试,如果您以任何方式对UIViewController进行了子类化,或者正在使用UITableViewController或UINavigationController,请确保在相关选项卡的检查器中设置了正确的对象类型 - 由于遗忘,我没有遇到任何问题在过去这样做。

答案 1 :(得分:0)

你试过[self reloadData]吗?考虑到它是你想要更新的UITableView FirstViewController。