zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Eclipse中SVN插件的安装方式
    Javascript实现DIV滚动自动滚动到底部
    Android程序开发的环境配置
    Android程序开发基础之——页面布局
    TinyMCE使用手册
    PHP中使用mktime获取时间戳的一个黑色幽默
    VS11本地IIS调试时(URL不使用虚拟目录,直接用localhost)
    [VS扩展工具] Image Optimizer(图像优化压缩)
    [程序安装包制作] Advanced Installer 备忘
    安装VS2010 SP1时遇到WCF RIA Service 版本错误
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3651697.html
Copyright © 2011-2022 走看看