iphone标签栏应用查看生命周期问题

时间:2010-10-08 17:00:24

标签: iphone cocoa-touch uitabbarcontroller uitableview

这可能是一个容易回答的愚蠢问题。我是iOS开发人员的新手,这让我很伤心。

我有一个基本的标签栏应用。该应用程序在启动时加载初始选项卡,它是一个tableview控制器。该tableview的数据由datacontroller提供,它从互联网上获取的数据创建一个数组。当我第一次启动应用程序时,在tableview中没有显示任何内容,它是空的。但是,如果我选择一个不同的选项卡,然后返回到该选项卡,数据现在就会出现并显示在tableview中。如何让数据第一次显示?它显然在那里。我相信这可能与导致问题的数据加载的位置/时间有关,任何见解都会很棒。

这会在app delegate中设置datacontroller:

// Create Locations data controller.
LocationDataController *controller = [[LocationDataController alloc] init];
self.locationDataController = controller;
[controller release];
locationsViewController.locationDataController = locationDataController;

这是datacontroller代码:

@implementation LocationDataController
@synthesize fetchConnection = mFetchConnection;
@synthesize locations, tempLocations;



-(id)initData {
    if (self = [super init]) {
        [self createData];
    }
    return self;
}

-(id)init {
    [self fetchData];
    return self;
}


-(unsigned)countOfList {
    return [locations count];
}


-(Location *)objectInListAtIndex:(unsigned)theIndex {
    return [locations objectAtIndex:theIndex];
}


-(void)dealloc {
    [locations release];

    if (mFetchConnection != nil) {
        [mFetchConnection cancel];
        [mFetchConnection release];
    }

    if (mFetchConnectionBuffer != nil)
        [mFetchConnectionBuffer release];

    [super dealloc];
}


#pragma mark -
#pragma mark Fetching/Sending Data
#pragma mark

- (void)fetchData {
    if (mFetchConnection == nil) {
        if (mFetchConnectionBuffer != nil)
            [mFetchConnection release];

        mFetchConnectionBuffer = [[NSMutableData alloc] init];

        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:kFetchURLPath]];
        self.fetchConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease];
        [request release];
    }
}




#pragma mark -
#pragma mark NSURLConnection Delegate
#pragma mark

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)inData {
    if (connection == mFetchConnection) {
        [mFetchConnectionBuffer appendData:inData];
    }
}




- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)connectionError {
    if (connection == mFetchConnection) {
        self.fetchConnection = nil;
        [mFetchConnectionBuffer release];
        mFetchConnectionBuffer = nil;
    }
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    tempLocations = [[NSMutableArray alloc] init];

    if (connection == mFetchConnection) {
        NSString *str = [[NSString alloc] initWithData:mFetchConnectionBuffer encoding:NSUTF8StringEncoding];
        SBJSON *parser = [[SBJSON alloc] init];
        id result = [parser objectWithString:str error:nil];
        if (result != nil && [result isKindOfClass:[NSArray class]])
            [tempLocations setArray:result];
        [str release];
        [mFetchConnectionBuffer release];
        mFetchConnectionBuffer = nil;
        self.fetchConnection = nil;

        [self initData];

    }
}


-(void)createData {

    // Cycle through array to implement locations.
    Location *location;
    NSMutableArray *array = [[NSMutableArray alloc] init];

    // create arry of locations.
    for (int i=0; i<[tempLocations count];i++) {

        location = [[Location alloc] init];
        location.name = [[tempLocations objectAtIndex:i] objectForKey:@"name"];
        location.address = [[tempLocations objectAtIndex:i] objectForKey:@"address"];
        location.city = [[tempLocations objectAtIndex:i] objectForKey:@"city"];
        location.state = [[tempLocations objectAtIndex:i] objectForKey:@"state"];
        location.zip = [[tempLocations objectAtIndex:i] objectForKey:@"zip"];

        [array addObject:location];
        [location release];

    }

    self.locations = array;
    [array release];
    [tempLocations release];

}

1 个答案:

答案 0 :(得分:0)

假设您已正确实现了TableView委托方法,我建议您在创建数据后调用tableView.reloadData以查看是否有效。