ilhnctn
ilhnctn

Reputation: 2210

UITableView delegate fires NSURLConnection delegate

i fetch data via NSURLConnction from a server and want to populate a tableview from fetched array. The data appears in the log from NSURLConnection delegate method but i realized that numberOfRowsInSection of UITableView delegate method(DM) fires before

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {}

DM of NSURLConnection. and this causes a problem for me because even tough i get the data properly returned number of rows is allways 0(zero).. How can i solve this issue please share any idea.. Thanks

Upvotes: 1

Views: 235

Answers (2)

Moxy
Moxy

Reputation: 4212

You need to call [tableView reloadData]; at the end of -connection:didReceiveData: in order to update your table view.

Edit: Bartu is right! You need to call it at the end of -connectionDidFinishLoading:

Upvotes: 1

Bartu
Bartu

Reputation: 2210

didRecieveData might be called more than once, and does not indicates that all data is fetched. You should implement a private property on your class such as;

@property (nonatomic,retain) NSMutableData tableData;

and on your didRecieveData;

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   [self.tableData appendData:data];
}

After that when your connection is closed

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // use tableData and refresh table...
    [self.tableView reloadData];
}

Upvotes: 2

Related Questions