【发布时间】:2011-06-11 06:25:52
【问题描述】:
我正在编写一个与 Web 服务集成的 iPhone 应用程序。我将从 Web 服务获取数据并用它填充 tableview。我的问题:当用户滚动表格视图时,我希望更多数据从 Web 服务动态加载并填充表格视图。
对此有什么想法吗? 非常感谢!
【问题讨论】:
标签: iphone uitableview
我正在编写一个与 Web 服务集成的 iPhone 应用程序。我将从 Web 服务获取数据并用它填充 tableview。我的问题:当用户滚动表格视图时,我希望更多数据从 Web 服务动态加载并填充表格视图。
对此有什么想法吗? 非常感谢!
【问题讨论】:
标签: iphone uitableview
Facebook 的three20 库有一个TTTableViewController 和TTTableViewDataSource,可让您从Internet 加载您的内容。我想这就是你要找的。del>
UPDATEthree20 似乎已不再维护,因此请忽略上面的链接。下面的答案应该足够了。
如果您希望自己做事而不是使用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];
}
【讨论】: