【问题标题】:Load more data from a web service when the user scrolls the UITableView当用户滚动 UITableView 时从 Web 服务加载更多数据
【发布时间】:2011-06-11 06:25:52
【问题描述】:

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

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

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    Facebook 的three20 库有一个TTTableViewControllerTTTableViewDataSource,可让您从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];
    }
    

    【讨论】:

    • 如果您不想使用three20,请添加有关如何处理的段落。
    • 谢谢。对我有用!
    • 你可以展示一些代码示例使用 -tableView:willDisplayCell:forRowAtIndexPath:
    • 刚刚添加了一些示例代码——应该足以让您入门。
    • 您好,我正在使用最后一个单元格中的加载更多按钮在检索数据后从 Web 服务中检索更多数据如何删除加载更多按钮单元格并使用更新的数据源更新表格视图。谢谢你的建议。
    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多