zoukankan      html  css  js  c++  java
  • UITableView分页加载--仿照网易新闻--点击加载更多

    原理:先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 比如:



    数据源是个array:

     

    NSMutableArray *items;

    ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

            int count = [items count];

        return  count + 1;

    }

     

      这个方法定制cell的显示, 尤其是"加载更多"的那个cell:

     

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        if([indexPath row] == ([items count])) {

            //创建loadMoreCell

            return loadMoreCell;

        }

     

        //create your data cell

     

        return cell;

    }

     

      还要处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表

     

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     

        if (indexPath.row == [items count]) {

            [loadMoreCell setDisplayText:@"loading more ..."];

            [loadMoreCell setAnimating:YES];

            [self performSelectorInBackground:@selector(loadMore) withObject:nil];

            //[loadMoreCell setHighlighted:NO];

            [tableView deselectRowAtIndexPath:indexPath animated:YES];

            return;

        }

     

        //其他cell的事件

     

    }

     

      加载数据的方法:

     

    -(void)loadMore

    {

        NSMutableArray *more;

            //加载你的数据

        [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];

    }

     

      添加数据到列表:

     

    -(void) appendTableWith:(NSMutableArray *)data

    {

     

        for (int i=0;i<[data count];i++) {

            [items addObject:[data objectAtIndex:i]];

        }

        NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];

        for (int ind = 0; ind < [data count]; ind++) {

            NSIndexPath    *newPath =  [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];

            [insertIndexPaths addObject:newPath];

        }

        [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];

     

    }

     

        我这里是写死的数据,作例子,在实现工程项目中,比如:要向服务器请求数据,一般返回数据会有总共多少页,一次请求一页,当用户需要看更多页面时,滑动到最下面cell,再请求下一面!

     

       我这里例子,请求加载更多是写到didSelectRowAtIndexPath:代理方法中,如果需要自动加载,可以放到- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    代理方法中。
  • 相关阅读:
    如何:在 DataGrid 控件中对数据进行分组、排序和筛选
    转载:一站式WPF依赖属性(DependencyProperty)
    转载:WPF 善用路由事件
    Tomcat内存溢出
    Postgresql允许远程访问配置修改
    python笔记2
    我怀念的
    OpenCV第二个assignment:检测QR code的3个 finder centers
    学期总结
    机器视觉课程的第一个assignment——OpenCV
  • 原文地址:https://www.cnblogs.com/xukunhenwuliao/p/3576236.html
Copyright © 2011-2022 走看看