当用户滚动UITableView时,从Web服务加载更多数据

don*_*gmh 2 iphone uitableview

我正在编写一个与Web服务集成的iPhone应用程序.我将从Web服务获取数据并用它填充tableview.我的问题:当用户滚动tableview时,我希望从Web服务动态加载更多数据并填充tableview.

有什么想法吗?非常感谢!

Cha*_*pta 6

Facebook的three20库有一个TTTableViewControllerTTTableViewDataSource,允许您从Internet加载内容.我认为这就是你要找的东西.

UPDATE three20,似乎已不再维护,因此忽略上面的链接.以下答案应该足够了.

如果你正在寻找做的事情你自己,而不是使用three20,那么你就可以实现的UITableViewDelegate-tableView:willDisplayCell:forRowAtIndexPath:在你的表视图控制器.当用户滚动到表视图中的最后一个(部分,行)(您可以从索引路径中获知)时,只需进行异步http调用并在内容到达时重新加载表视图.

// 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];
}
Run Code Online (Sandbox Code Playgroud)