DZLoadMore 0.1.1

DZLoadMore 0.1.1

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布上次发布2015年7月

Danis Ziganshin 维护。




  • 作者:
  • Danis Ziganshin

无限滚动加载更多条目在 UITableView,带有内置的 “下拉刷新” 功能。

我已经厌倦了在每个项目上添加 “加载更多” 和 “下拉刷新” 功能。所以我决定将我的代码移动到单个类的单个类中,其中包括两个处理加载和刷新条目的好方法。这也有助于我停止在单个 UIViewController 子类中实现表格的数据源和代理。

屏幕截图

使用方法

  1. 继承 DZLoadMore
  2. 实现 -(NSArray *)items 方法。您应该在方法中返回您的 UITableView 的模型项目数组。
  3. 添加以下表格数据源协议方法

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            // Your tableView:cellForRowAtIndexPath: implementation
        }
        ...
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
        if (height == NSNotFound) {
            // Your tableView:heightForRowAtIndexPath: implementation
        }
        return height;
    }
  4. 在您的 ViewController 子类中:使用您新创建的类的对象设置并设置您的 UITableView 的数据源属性。

    self.dataSource = [[MyDataSource alloc] init];
    self.tableView.dataSource = self.dataSource;
  5. 如果您需要 加载更多 功能:设置数据源的数据源为 loadMoreItemsBlock 属性。

    __weak typeof(self) weakSelf = self;
    self.dataSource.loadMoreItemsBlock = ^(id lastItem, BOOLCallback block) {
        MyObject *object = (MyObject *)lastItem;
        // Asyncronously load new items from your backend. You should provide fininsh block that should have BOOL value indicating if server doesn't have any more items to load
        [weakSelf loadItemsFromItem:object finishBlock:^(BOOL noMoreItems) {
            block(noMoreItems); // You should invoke this block after you've updated your dataSource with new values
            [weakSelf.tableView reloadData];
        }];
    };
  6. 如果您需要 下拉刷新 功能:将数据源为 refreshContentBlock 属性,并将您 UITableViewController 的 refreshControl 属性设置为目标数据和源及 -refreshContent 选择器。

    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self.dataSource action:@selector(refreshContent) forControlEvents:UIControlEventValueChanged];
    __weak typeof(self) weakSelf = self;
    self.dataSource.refreshContentBlock = ^(BOOLCallback block) {
        // Asyncronously load new items from your backend. You should provide fininsh block that should have BOOL value indicating if server doesn't have any more items to load
        [weakSelf loadItemsFromItem:nil finishBlock:^(BOOL noMoreItems) {
            [weakSelf.refreshControl endRefreshing]; // Your UITableViewController responsible for invoking -endRefreshing method or UIRefreshControl
            block(noMoreItems); // You should invoke this block after you've updated your dataSource with new values
            [weakSelf.tableView reloadData];
        }];
    };

安装

pod 'DZLoadMore'

许可

MIT