Reputation: 21
I'm writing an iPhone app that is integrated with a web service. I will get data from the web service and fill the tableview with it. My issue: when the user scrolls the tableview I want more data to load dynamically from the web service and fill the tableview.
Any ideas for that? Thanks so much!
Upvotes: 2
Views: 3745
Reputation: 4053
Facebook's three20 library has a TTTableViewController and TTTableViewDataSource which allows you to load your content from the Internet. I think that is what you are looking for.
UPDATE three20, it seems, is no longer maintained, so ignore the links above. The answer below should be sufficient.
If you are looking to do things yourself rather than use three20, then you can implement UITableViewDelegate's -tableView:willDisplayCell:forRowAtIndexPath:
in your table view controller. When the user scrolls to the last (section,row) in your table view (which you can know from the index path), just make an asynchronous http call and reload the table view when content arrives.
// YourTableViewController.m
// Assuming your table view's controller is a subclass of UITableViewController
// if its not, you will need to manually set your table view's delegate to this class
// i.e. self.tableView.delegate = self;
// if a table view's delegate implements -tableView:willDisplayCell:forRowAtIndexPath:
// the table view will call this method before displaying any cell
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == lastSection && indexPath.row == lastRowInLastSection) {
// assuming you use ASIHTTPRequest
NSURL *url = [NSURL URLWithString:@"http://your-webservice.example.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
}
// ASIHTTPRequest calls this method when it gets a response for a HTTP request
- (void)requestFinished:(ASIHTTPRequest *)request {
// you can get the response data from
// [request responseString] or
// [request responseData]
...update your data source...
[self.tableView reloadData];
}
Upvotes: 6