UITableView 使用方法(一) - 创建简单的列表

114 篇文章 0 订阅
100 篇文章 0 订阅


UITableView是iPhone中比较常用的,用的比较多的控件,下面我们使用UITableView创建一个简单的表格,效果如下:


如果要表格中增加数据的话,需要增加UITableViewDataSource协议。

如果需要响应用户单击的话,需要增加UITableViewDelegate协议。


1、创建项目:使用模板Single View Application新建一个项目,仅支持iPhone。

2、在ViewController.h中增加UITableViewDataSource和UITableViewDelegate协议,如下:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {

    NSArray * listData;
}

@property ( nonatomic, retain) NSArray *listData;

@end

3、往列表中增加数据,实现UITableViewDataSource协议,如下:

//返回总行数
-(NSInteger ) tableView:(UITableView *)tableView

  numberOfRowsInSection:(NSInteger )section

{    
    return [ self.listData count ];
    
}

// 添加每一行的信息
- (UITableViewCell *) tableView:(UITableView *)tableView
          cellForRowAtIndexPath:(NSIndexPath *)indexPath

{   
    
    NSString *tag=@"tag";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];
    
    if (cell==nil ) {
        cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero
                                        reuseIdentifier:tag] autorelease];
    }    
    
    NSUInteger row=[indexPath row];
    
    //设置文本
    cell.text =[listData objectAtIndex :row];
    
    //选中后的颜色又不发生改变,进行下面的设置
    //cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    
    //不需要分割线
    //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;  
    
    return cell;
    
}

4、响应用户单击事件,实现UITableViewDelegate协议,如下:

//响应用户单击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    
    UIAlertView* showSelection;
    NSString* message;
    
    message = [[NSString alloc]initWithFormat:@"You chose the : %@",
                     [self.listData objectAtIndex:indexPath.row]];
    
    showSelection = [[UIAlertView alloc]
                     initWithTitle:@"Selected"
                     message:message
                     delegate:nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];   
    
    [showSelection autorelease];
    [showSelection show];
}

5、往ViewController中增加UITableView,并将UITableView的delegate和dataSource连接到ViewController。如下图所示:

































6、完整的代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize listData;

- (void)viewDidLoad
{
    
    self.listData =[[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3", @"Item4", @"Item5", @"Item6", @"Item7",nil];;

    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    self.listData = nil;
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


#pragma mark - Table view data source delegate

//返回总行数
-(NSInteger ) tableView:(UITableView *)tableView
  numberOfRowsInSection:(NSInteger )section
{    
    return [ self.listData count ];
}

// 添加每一行的信息
- (UITableViewCell *) tableView:(UITableView *)tableView
          cellForRowAtIndexPath:(NSIndexPath *)indexPath

{   
    
    NSString *tag=@"tag";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];
    
    if (cell==nil ) {
        cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero
                                        reuseIdentifier:tag] autorelease];
    }    
    
    NSUInteger row=[indexPath row];
    
    //设置文本
    cell.text =[listData objectAtIndex :row];
    
    //选中后的颜色又不发生改变,进行下面的设置
    //cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    
    //不需要分割线
    //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;  
    
    return cell;
    
}

#pragma mark - Table view data delegate

//响应用户单击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    
    UIAlertView* showSelection;
    NSString* message;
    
    message = [[NSString alloc]initWithFormat:@"You chose the : %@",
                     [self.listData objectAtIndex:indexPath.row]];
    
    showSelection = [[UIAlertView alloc]
                     initWithTitle:@"Selected"
                     message:message
                     delegate:nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];   
    
    [showSelection autorelease];
    [showSelection show];
}

@end

附上实例源代码下载:

http://download.csdn.net/detail/ztp800201/4500391

编译环境:Mac OS 10.7.4 + XCode4.3.3 + iOS5.1








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值