栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

似乎您不是在考虑MVC。您的UITableView与分页和webrequest几乎没有关系。它只关心其数据源而不是页面。

Restuful API设计:假设您的Web请求设计如下:

/ getRecorods?pageSize = 20&pageNo = 2

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

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

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

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

-(void)viewDidLoad{    [super viewDidLoad];    //加载视图后,通常从笔尖进行其他任何设置。    //在此处调用http Util加载数据    httpUtil.delegate =自我;    //这总是检索首页的帖子    currentPageNumber = 1;    [httpUtil getRecords:currentPageNumber];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)节{    //返回该节中的行数。    int retValue = 0;    if(recordsArray!= nil){        retValue = [recordsArray计数];    }    返回retValue;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    静态NSString * CellIdentifier = @“ Cell”;    CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if(cell == nil){    }    //使用recordsArray objectAtIndex配置单元格    返回单元}-(void)scrollViewDidScroll:(UIScrollView *)scrollView {      if(self.tableView.contentOffset.y> =(self.tableView.contentSize.height-self.tableView.bounds.size.height)){      // NSLog(@“滚动到底部!”);      if(isPageRefresing == NO){//不必担心线程,因为它始终在主线程上。        isPageRefresing = YES;        [self showMBProfressHUDOnView:self.view withText:@“请稍候...”];        currentpagenumber = currentpagenumber +1;        [httpUtil getRecords:currentpagenumber];       }    }}//您可以从标记获取pageNo//确保在主线程中调用了此方法-(void)didFinishRecordsRequest:(NSArray *)结果为Page:(NSInteger)pageNo {    if(pageNo == 1){        recordsArray = [results mutableCopy];    }    其他{        [recordsArray addObjectsFromArray:results];    }    isPageRefresing = NO;    [self.tableView reloadData];}-(void)didFailedChalkBoardRequestWithError:(NSError *)错误{    //如果由于后续刷新调用(对于第2页或第3页)失败    //然后撤消当前页码    currentpagenumber--;    isPageRefresing = NO;}// HTTP Utility类-(void)getRecords:(NSInteger)pageNumber {    NSString * serverUrl = [NSString stringWithFormat:@“ http://yourwebsite.com/page/%d /?json”,pageNumber];    NSLog(@“使用URL%@从服务器获取故事数据”,serverUrl);    NSURL * url = [NSURL URLWithString:serverUrl];    storyRequest = [ASIHTTPRequest requestWithURL:url];    storyRequest.tag = pageNumber;    [storiesRequest setDelegate:self];    [storiesRequest startAsynchronous];}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号