UITableView实现分页显示

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

实现的思路:

  • 基本上就是数据源里先只放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/





### Web 应用开发项目课程设计示例 #### 1. 项目概述 Web应用开发主要是在B/S架构模式下构建一系列基于浏览器的应用程序[^1]。对于一个完整的Web应用开发课程设计,通常会围绕创建一个实际可用的小型Web应用程序展开。 #### 2. 需求分析 假设要开发的是一个简易版的电子商务网站,该平台允许用户浏览商品列表并购买心仪的商品。此过程涉及多个方面的工作,包括但不限于UI界面的设计、前后端逻辑实现等。 #### 3. 技术选型 - **前端框架**: 可选用React.js 或 Vue.js 来提高用户体验。 - **后端服务**: Node.js配合Express框架可以快速搭建RESTful API服务器。 - **数据库管理**: MongoDB作为NoSQL数据库能够灵活存储非结构化的数据。 - **版本控制工具**: Git用于团队协作中的代码管理和变更追踪。 ```javascript // 示例:使用Node.js 创建简单HTTP服务器 const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(8080, () => { console.log(`Server running at http://localhost:8080/`); }); ``` #### 4. 功能模块划分 整个项目可被划分为几个核心功能模块: - 用户注册登录系统 - 商品展示页面 - 购物车管理系统 - 订单处理流程 每个模块都应有清晰的功能描述和技术栈说明,以便于后续的具体实施工作。 #### 5. 实施计划 制定详细的进度表来规划各个阶段的任务完成时间点,确保按时交付高质量的产品原型。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值