• UITableView分页


    UITableView分页上拉加载简单,ARC环境,源码如下,以作备份:

    原理是,点击最后一个cell,触发一个事件来处理数据,然后reloadData

    RootViewController.m + RootViewController.h

    #import "RootViewController.h"
    
    @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
    
    @property (nonatomic, strong) UITableView     *tableView;
    @property (nonatomic, strong) NSMutableArray  *dataSource;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        _dataSource = [[NSMutableArray alloc] init];
        for (int i = 0; i < 10; i++)
        {
            [_dataSource addObject:[NSString stringWithFormat:@"%d", i]];
        }
        
        _tableView  = [[UITableView alloc] initWithFrame:self.view.bounds
                                                   style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        [self.view addSubview:_tableView];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // 因为要定制最后一行,所以cell多一个
        return _dataSource.count + 1;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *reusedStr = @"demo";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:reusedStr];
        }
        
        if([indexPath row] == ([_dataSource count]))
        {
            // 定制最后一行的cell
            cell.textLabel.text=@"获取更多..";
        }
        else
        {
            // 定制普通行的cell
            cell.textLabel.text=[_dataSource objectAtIndex:[indexPath row]];
        }
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 如果是最后一行点击事件,则触发一个事件
        if (indexPath.row == ([_dataSource count]))
        {
            [self performSelectorInBackground:@selector(loadMore)
                                   withObject:nil];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            return;
        }
    }
    
    - (void)loadMore
    {
        // 添加数据源
        for (int i = 0; i < 10; i++) {
            [_dataSource addObject:[NSString stringWithFormat:@"新%d", i]];
        }
        
        // 重新加载tableView
        [_tableView reloadData];
    }
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
  • 相关阅读:
    FFmpeg4.0 的filter机制的架构与实现.之二 结构体关系与定义
    ffmpeg filter过滤器 基础实例及全面解析
    Filter初始化 filter
    FFmpeg4.0 的filter机制的架构与实现.之三 Filter实现的源码分析
    C语言结构体初始化的四种方法
    ChilliCream 是一个开源免费的 GraphQL 平台
    十道算法题
    速战速决 Python python 基础: 函数
    速战速决 Python python 数据类型: 集合类型
    速战速决 Python python 高级: 迭代器,生成器
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3651697.html
Copyright © 2020-2023  润新知