开发者

Why isn't my UITableViewController being populated here?

开发者 https://www.devze.com 2023-04-06 12:50 出处:网络
Just trying to understand my table isn\'t being populated when my view controller appears here. I\'m using AIHTTPRequest to communicated with a REST API. The call returns an array of associative array

Just trying to understand my table isn't being populated when my view controller appears here. I'm using AIHTTPRequest to communicated with a REST API. The call returns an array of associative arrays (which I'm assuming I'll have to cast to an NSArray of NSDictionaries).

In the header of my T开发者_开发知识库ableViewController:

@interface MyTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    NSArray *homepageItems;
}

@property (nonatomic, retain) NSArray * homepageItems;

In the implementation:

@synthesize homepageItems;
....
- (void)viewDidLoad {

NSURL *url = [NSURL URLWithString:URL_TO_MY_API];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

When the data is returned:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    homepageItems = (NSArray *)response;
}

And in the Table View method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    NSDictionary *dataItem = [homepageItems objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = [dataItem objectForKey:@"message"];

    return cell;
}

When the view loads nothing is in the table. I suspect this is happening because when the table view method is fired homepageItems hasn't been populated by the asynchronous method. How can I achieve the data being loaded into the table view?

Thanks,

EDIT: In my requestFinished method, I've examined the homepageItems object by doing the following:

for(NSDictionary *dictionary in homepageMMDs) {
    NSLog(@"%@", [dictionary objectForKey:@"message"]);
}

And the proper messages from my API service are being logged.


You need to retain the homepageItems

 self.homepageItems = (NSArray *)response;

And also reload the tableview as Joe said


Try reloading the data!

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];

    //Use self. to properly retain and you do not need to cast id assignments
    self.homepageItems = response; 

    //Make sure you reload the data!!
    [self.tableView reloadData];
}


You are not retaining the downloaded data:

Instead of:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data.
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    homepageItems = (NSArray *)response;
}

You need to do: (crucially using self in front of homepageItems)

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data.
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    self.homepageItems = (NSArray *)response; 
    // and others have said you need to reload the tableView
    [self.tableView reloadData];
}


You can use this cocoapod https://github.com/xmartlabs/XLData in order to fetch data from an API endpoint and populate a UITableView/UICollectionView.


I suggest you to create an php page which displays your API strings,before you can parse the elements and import they to wherever you want,its much easier!Related to your code i suppose you need to parse the element,as you can se here : http://www.ibm.com/developerworks/library/x-ioschat/. Lucky.

0

精彩评论

暂无评论...
验证码 换一张
取 消