gpt4 book ai didi

iphone - iOS - 如何使 tableview 使用分页 API 输出?

转载 作者:行者123 更新时间:2023-12-03 18:18:49 30 4
gpt4 key购买 nike

我正在编写的 API 大约有 2000 条记录,通过我编写的简单 RESTful API 以 JSON 形式返回。

为了减少大量数据的问题,我想使用分页,这样我就可以通过 offsetlimit 等方式返回每个请求的前 10 个或前 20 个数据或页面

但我的问题是 iOS UITableView 如何知道何时获取下一页结果?

我真的不知道该怎么做。用户可能滚动速度超快,因此 API 可能没有足够的时间一次检索 20 或 50 条记录。

与此相关的另一个问题是,假设用户在 UITableView 上向下滚动,然后向上滚动,然后再次向下滚动 - 如何防止 API 对同一行多次触发?

谢谢

最佳答案

看来您没有考虑 MVC 。你的 UITableView 与分页和网络请求关系不大。它只关心它的数据源而不是页面。

Restful API 设计:假设您的网络请求设计如下:

/getRecorods?pageSize=20&pageNo=2

这将返回一个 JSON 数组。除此之外,拥有计数参数和下一页的链接也很有帮助。这有助于解析并与网络服务器同步。

如何防止 API 对同一行多次触发?

一个简单的标志就足以避免加载多个页面。只需确保在主线程中访问该标志即可。实际的网络请求需要进入后台线程。

下面是您需要放入 UITableViewController 中加载数据的代码

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

关于iphone - iOS - 如何使 tableview 使用分页 API 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22528800/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com