0

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 TableViewController:

@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.

1
  • Are you sure that the data you download is directly an array, and not an array in a dictionary or something ? Commented Sep 19, 2011 at 21:47

5 Answers 5

2

You need to retain the homepageItems

 self.homepageItems = (NSArray *)response; 

And also reload the tableview as Joe said

Sign up to request clarification or add additional context in comments.

Comments

2

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]; } 

7 Comments

Hmm, doesn't seem to be any difference
Well now you need to make sure homepageItems is an array, not nil and the count is greater than 0, also make sure the elements are dictionaries and have a non empty value for the key message. Start setting breakpoints and logging values.
Have you updated the numberOfRowsInSection to be the size of the array?
I've set the number of rows, sorry I should have included that in the post. In the requestFinished method I've set a breakpoint and can see the data coming back. However in the cellforRow method, it doesnt look homepageItems is populated yet or at all.
@barfoon: Add a minimal example of what you logged for the homePageItems object.
|
1

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]; } 

4 Comments

This work, however when I scroll the table view crashes. Im guessing there is a retain issue somewhere?
'unrecognized selector sent to instance 0x12dc5e8'. Happens when I assign cell.textLabel.text
That would suggest that dataItem is not an NSDictionary. Can you post the stack trace? (when it crashes type bt in the debugger console. (after the (gdb))
Looks like it was a problem with a character in message being returned. Ill have to investigate that separately. Thanks @Jonathan.
0

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

Comments

-1

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.

1 Comment

The API call is working - the responseString is correctly appearing when it is assigned.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.