• [转]UITableView 实现分页显示的代码


    UITableview 能够列表显示许多内容,也是我们开发中经常用的一个组件。我们经常会分页显示列表,如先显示 10 条记录,点击更多在添加 10 条,以此类推,下面是实现类似更多显示的一个 demo。

        实现的效果如下:

        点击 “More…”,实现后面的效果.

        实现的思路:

    • 基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中.。
    • 处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表。
    • indexPathForRow插入数据。

        实现过程如下:

    #import <UIKit/UIKit.h>

    @interface iphone_tableMoreViewController : UIViewController
    <UITableViewDelegate,UITableViewDataSource>{
       
        IBOutlet UITableView *myTableView;
        NSMutableArray *items;
    }
    @property (nonatomic,retain) UITableView *myTableView;
    @property (nonatomic,retain) NSMutableArray *items;
    @end

    #import "iphone_tableMoreViewController.h"
    @implementation iphone_tableMoreViewController
    @synthesize items,myTableView;
    - (void)viewDidLoad {
        [super viewDidLoad];
        items=[[NSMutableArray alloc] initWithCapacity:0];
        for (int i=0; i<10; i++) {
            [items addObject:[NSString stringWithFormat:@"cell %i",i]];
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    - (void)viewDidUnload {
        items=nil;
        self.myTableView=nil;
    }
    - (void)dealloc {
        [self.myTableView release];
        [items release];
        [super dealloc];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        int count = [items count];
        return  count + 1;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *tag=@"tag";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];
        if (cell==nil) {
            cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
                                         reuseIdentifier:tag] autorelease];
        }   
        if([indexPath row] == ([items count])) {
            //创建loadMoreCell
            cell.textLabel.text=@"More..";
        }else {
        cell.textLabel.text=[items objectAtIndex:[indexPath row]];   
        }
        return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       

        if (indexPath.row == [items count]) {
            UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath];
            loadMoreCell.textLabel.text=@"loading more …";
            [self performSelectorInBackground:@selector(loadMore) withObject:nil];
           [tableView deselectRowAtIndexPath:indexPath animated:YES];
            return;
        }
        //其他cell的事件
       
    }
    -(void)loadMore
    {
        NSMutableArray *more;
        more=[[NSMutableArray alloc] initWithCapacity:0];
        for (int i=0; i<10; i++) {
            [more addObject:[NSString stringWithFormat:@"cell ++%i",i]];
        }
        //加载你的数据
        [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
        [more release];
    }
    -(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.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
       
    }
    @end

    源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.tableMore/

  • 相关阅读:
    python经常使用的十进制、16进制、字符串、字节串之间的转换(长期更新帖)
    axis2开发webservice之编写Axis2模块(Module)
    Android中的动画具体解释系列【2】——飞舞的蝴蝶
    湘潭邀请赛——Alice and Bob
    ZOJ 2859 二维线段树
    jsp导出身份证到excel时候格式不正确
    Android 自己定义View须要重写ondraw()等方法
    聚合类新闻client产品功能点详情分析
    vi下对齐代码的操作
    最新研发的基于Java的高速开发平台
  • 原文地址:https://www.cnblogs.com/wellsoho/p/2626826.html
Copyright © 2020-2023  润新知