zoukankan      html  css  js  c++  java
  • iOS-MJRefresh框架

     1.用MJRefresh框架实现上下拉刷新

    1.1 如何使用这个框架,只需要告诉控件的scrollView是谁,就能将框架添加到我们的滚动视图中了

    // 下拉刷新

       MJRefreshHeaderView *header = [MJRefreshHeaderView header];

       header.scrollView = self.tableView;

       header.delegate = self;

       // 开始下拉刷新

       [header beginRefreshing];

    / 下拉刷新

       MJRefreshFooterView *footer = [MJRefreshFooterView footer];

       footer.scrollView = self.tableView;

       footer.delegate = self;

    1.2 如何加载数据:需要知道两个参数的意思 sinceId 和 maxId ,两个不能同时传,如果传入sinceId,就会新浪服务器就会返回比sinceId大的微博数据,也就是最新的,所以我们只需要取出当前数组最大的sinceId也就是数组第一关元素的sinceId传入到请求方法中,就能获取。如果传入maxId,新浪服务器就会返回小于等于maxId的微博数据,如果想要获取以前的数据,并且不重复,需要取出数组中最小的maxId,也是最后一个元素的maxId将maxId-1,就能获取了之前的数据了。

    1.3 加载最新数据,需要将获取到的数据插入最前面

    #pragma mark 下拉刷新新数据

    - (void)loadNewData:(MJRefreshBaseView *)refreshView

    {

       // 取出第一条微博ID

       // 需要判断数组是否为空。

       StatusCellFrame *f = _statusesFrame.count?_statusesFrame[0] :nil;

       long long sinceId = f.status.ID;

       [StatusTool statusesWithPath:@"2/statuses/friends_timeline.json"  sinceId:sinceId maxId:0 success:^(NSArray *statues) {

           // 显示刷新了多少条数据

           [self showNewStatusCount:statues.count];

     // 将获取到的数据放置最前面

           NSMutableArray *newFrames = [NSMutableArray arrayWithCapacity:statues.count];

           for (Status *status in statues) {

               // 将微博数据转换成frame模型

               StatusCellFrame *f = [[StatusCellFrame alloc] init];

               f.status = status;

               [newFrames addObject:f];

           }

           // 将数据插入到数组最前面

           [_statusesFrame insertObjects:newFrames atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newFrames.count)]];

           

           // 刷新表格

           [self.tableView reloadData];

           

           // 停止刷新状态

           [refreshView endRefreshing];

           

       } failure:^(NSError *error) {

           

           [refreshView endRefreshing];

           

       }];

       

    }

    1.4 加载更多以前的数据,直接将获取到的数据添加到最后就行了。

    #pragma mark 上啦刷新更多

    - (void)loadMoreData:(MJRefreshBaseView *)refreshView

    {

       // 取出最后一条微博ID

       StatusCellFrame *f = [_statusesFrame lastObject];

       long long maxId = f.status.ID;

       

       // 加载比当前小的微博数据

       maxId--;

       

       [StatusTool statusesWithPath:@"2/statuses/friends_timeline.json"  sinceId:0 maxId:maxId success:^(NSArray *statues) {

           // 将获取到的数据放置最前面

           NSMutableArray *newFrames = [NSMutableArray arrayWithCapacity:statues.count];

           for (Status *status in statues) {

               // 将微博数据转换成frame模型

               StatusCellFrame *f = [[StatusCellFrame alloc] init];

               f.status = status;

               [newFrames addObject:f];

           }

           // 将数据添加到最后面

           [_statusesFrame addObjectsFromArray:newFrames];

           

           // 刷新表格

           [self.tableView reloadData];

           

           // 让刷新控件停止刷新状态

           [refreshView endRefreshing];

           

       } failure:^(NSError *error) {

           

           [refreshView endRefreshing];

           

       }];

    }
     
     
    代码地址: https://github.com/CoderMJLee/MJRefresh
  • 相关阅读:
    excel的导入导出
    mybatis常用sql
    java中和时间相关的类,方法
    <resultMap>
    项目启动报的错
    多表查询
    file的一些方法
    AOV网络与AOE网络
    封装解封装过程
    以太网交换机
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5055576.html
Copyright © 2011-2022 走看看