iOS版 - 如何让tableview中使用分页API输出? [英] iOS - How to make the tableview use paginated API output?

查看:228
本文介绍了iOS版 - 如何让tableview中使用分页API输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个API大约有2000条记录,通过我写了一个简单的RESTful API返回的JSON。

An API I am writing has about 2000 records, returned in JSON via a simple RESTful API I have written.

要减少大量数据的问题,我想使用分页,这样我只回说的第10或通过每个请求的前20个像偏移限制

To reduce issues with lots of data, I want to use pagination so that I only return say the first 10 or first 20 per request via like an offset or limit or page, etc.

但我的问题是如何在iOS版的UITableView 知道什么时候才能得到结果的翻页?

But my question is how does the iOS UITableView know when to get the next page of results?

我真的不确定如何做到这一点。用户可以滚动超快等等API可能没有足够的时间在一个时间来检索20或50的记录。

I really am unsure of how to do this. The user could be scrolling superfast and so the API might not have enough time to retrieve 20 or 50 records at a time.

与此相关的另一个问题是,假设用户滚动向下的UITableView,然后向上,然后再次回落 - ?你怎么了prevent烧制而成多次为同一行API

Another issue related to this is, lets say the user scrolls down on the UITableView, then up and then back down again -- how do you prevent the API from firing multiple times for the same rows?

感谢

推荐答案

好像是你是不是在MVC的角度思考。您的UITableView已经很少做寻呼和WebRequest的。这只是关心它的数据源不是网页。

Seems to be you aren't thinking in terms of MVC . Your UITableView has very little do with paging and webrequest. It's just concerned about its datasource not pages.

Restuful API设计:
假设你的Web请求被设计为如下:

Restuful API Design : Assume your web Request is designed as follow :

/ getRecorods的pageSize = 20安培;帮您做生意= 2

/getRecorods?pageSize=20&pageNo=2

这将返回JSON的数组。除了其有帮助的计数参数和到下一个页面的链接。这有助于分析和同步与Web服务器。

This will return you an Array of JSON. In addition to that its helpful to have a count parameter and a link to next page. This helps in parsing and sync with web server.

你怎么了prevent的API烧制而成多次为同一行?

how do you prevent the API from firing multiple times for the same rows?

一个简单的标志就足以避免加载多个页面。只要确保标志在主线程访问。实际的WebRequest需要后台线程去了。

A simple flag is sufficient to avoid loading multiple pages. Just make sure that flag is accessed in main thread. The actual webrequest needs to go in background thread.

下面是你需要把你的UITableViewController的code,它加载数据。

Below is the code you need to put into your UITableViewController which loads the data



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //call http Util here to load the data
    httpUtil.delegate = self;

    //This retrieves post for first page always
    currentPageNumber = 1;
    [httpUtil getRecords:currentPageNumber];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    int retValue = 0;
    if(recordsArray != nil){
        retValue = [recordsArray count];
    }
    return retValue;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

    }

    // Configure the cell using recordsArray objectAtIndex

    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

      if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height)) {

      //NSLog(@" scroll to bottom!");
      if(isPageRefresing == NO){ // no need to worry about threads because this is always on main thread.

        isPageRefresing = YES;
        [self showMBProfressHUDOnView:self.view withText:@"Please wait..."];
        currentpagenumber = currentpagenumber +1;
        [httpUtil getRecords:currentpagenumber];
       }
    }

}

// you can get pageNo from tag
// make sure this is called in main thread
-(void)didFinishRecordsRequest:(NSArray *)results forPage:(NSInteger)pageNo{
    if(pageNo == 1){
        recordsArray = [results mutableCopy];
    }
    else{
        [recordsArray addObjectsFromArray:results];
    }
    isPageRefresing = NO;
    [self.tableView reloadData];
}


-(void)didFailedChalkBoardRequestWithError:(NSError *)error{

    //If Since subsequent refresh calls(for page 2 or page 3 fails)
    //then undo the current page number
    currentpagenumber--;
    isPageRefresing = NO;
}

// HTTP Utility class
-(void)getRecords:(NSInteger)pageNumber{

    NSString *serverUrl = [NSString stringWithFormat:@"http://yourwebsite.com/page/%d /?json",pageNumber];

    NSLog(@"fetching Stories data from server WITH URL %@",serverUrl);
    NSURL *url = [NSURL URLWithString:serverUrl];
    storiesRequest = [ASIHTTPRequest requestWithURL:url];
    storiesRequest.tag = pageNumber;
    [storiesRequest setDelegate:self];
    [storiesRequest startAsynchronous];
}

这篇关于iOS版 - 如何让tableview中使用分页API输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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