0

jokesArray is a NSArray

This is my Data from Web service:

( { "_id" = { "$id" = 4e91fd49c7e24cda74000000; }; author = draper; comments = ( { author = adias; comment = "amazing again"; } ); created = { sec = 1318118400; usec = 0; }; text = "This is a random post again"; title = "post # 2"; type = ( punjabi ); }, { "_id" = { "$id" = 4e8cf1d6c7e24c063e000000; }; author = faisal; comments = ( { author = adias; comment = amazing; }, { author = nike; comment = "I concur"; } ); created = { sec = 1317772800; usec = 0; }; text = "This is a random post"; title = "post # 1"; type = ( punjabi ); } ) 

Please point me as to why my UITableView is not getting populated.

- (void)viewDidLoad { [super viewDidLoad]; jokesArray = [[NSArray alloc]init]; [self getJokes]; } -(void) getJokes { NSURL *url = [NSURL URLWithString:@"http://someurl"]; __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setCompletionBlock:^{ // Use when fetching text data NSString *responseString = [request responseString]; NSDictionary *resultsDictionary = [responseString objectFromJSONString]; jokesArray = [resultsDictionary allValues]; // Use when fetching binary data // NSData *responseData = [request responseData]; }]; [request setFailedBlock:^{ NSError *error = [request error]; }]; [request startAsynchronous]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [jokesArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // [[cell textLabel]setText:[jokesArray objectAtIndex:[indexPath row]]]; [[cell textLabel]setText:@"ok"]; return cell; } 

2 Answers 2

3

I think you need call reloadData method for your UITableView after assigned jokesArray:

jokesArray = [resultsDictionary allValues]; [jokesArray retain]; // May be need if this is not retained property [self.tableView reloadData]; 
Sign up to request clarification or add additional context in comments.

2 Comments

I did a : jokesArray = [[NSArray alloc]init]; Is that not enough to retain it?
But after assign jokesArray = [resultsDictionary allValues] you have new array. I think you have leak for old instance of jokesArray and have potential crash for new instance of jokesArray
1

try adding the following to you code after you get the array

[self.tableView reloadData]; 

You should add this because the table ask the delegate the row number on initialization and the response from your web service did not got back yet.

therefor, you should enforce the table to reload the data after you got your response and the delegate method will be called again!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.