当用户滚动UITableView时,从Web服务加载更多数据 [英] Load more data from a web service when the user scrolls the UITableView

查看:100
本文介绍了当用户滚动UITableView时,从Web服务加载更多数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

Facebook的 three20 库有 TTTableViewController TTTableViewDataSource 允许您从Internet加载内容。我认为这就是你要找的东西。

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.

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

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

这篇关于当用户滚动UITableView时,从Web服务加载更多数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆