简体   繁体   English

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

[英]Load more data from a web service when the user scrolls the UITableView

I'm writing an iPhone app that is integrated with a web service.我正在编写一个与 web 服务集成的 iPhone 应用程序。 I will get data from the web service and fill the tableview with it.我将从 web 服务获取数据并用它填充 tableview。 My issue: when the user scrolls the tableview I want more data to load dynamically from the web service and fill the tableview.我的问题:当用户滚动表格视图时,我希望从 web 服务动态加载更多数据并填充表格视图。

Any ideas for that?有什么想法吗? Thanks so much!非常感谢!

Facebook's three20 library has a TTTableViewController and TTTableViewDataSource which allows you to load your content from the Internet. Facebook 的three20库有一个TTTableViewControllerTTTableViewDataSource ,它允许您从 Internet 加载您的内容。 I think that is what you are looking for.我认为这就是你要找的。

UPDATE three20, it seems, is no longer maintained, so ignore the links above. UPDATE three20 似乎已不再维护,因此请忽略上面的链接。 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.如果你想自己做而不是使用three20,那么你可以在你的表视图controller 中实现UITableViewDelegate-tableView:willDisplayCell:forRowAtIndexPath: :。 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.当用户滚动到表格视图中的最后一个(部分,行)时(您可以从索引路径中知道),只需进行异步 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM