UITableView 基本使用

1、要使用UITableView必须用当前实现两个协议<UITableViewDataSource, UITableViewDelegate> UITableViewDataSource协议实现了数据加载的方法,UITableViewDelgate协议实现了UITableView外观设置,事件等方法。

01 // RootViewController.h 测试控制器
02
03 #import <UIKit/UIKit.h>
04
05 @interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
06 {
07 @private
08     UITableView *_tableView;
09     NSArray     *_listArray;
10 }
11 @end

_tableView 私有全局变量用来保存 UITableView对象。
_listArray   私有全局变量用来存放 UITableView需要的数据。

2、创建UITableView 

01 - (void)loadView {
02      
03     // 创建一个基本视图
04     UIView  *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
05     // 设置为当前控制器的默认视图
06     self.view = view;
07     // 释放视图
08     [view release];
09      
10     // 创建tableview
11     _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
12     // 设置数据源 注意这个方法设置不对,没有数据哦!
13     _tableView.dataSource = self;
14     // 添加到默认控制器中
15     [self.view addSubview:_tableView];
16     // tableview 获取系统默认字体数组 retain 是必须的,不然会报错哦!因为_listArray 必须接管这个数组对象
17     _listArray = [[UIFont familyNames] retain];   
18      
19 }
3、数据源方法
01 // 设置行数
02 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
03     return [_listArray count];
04 }
05
06 // 设置行数据
07
08 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
09     static NSString *cellIdentifier = @"cell";
10
11     UITableViewCell  *cell  = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
12      
13     if (cell == nil) {
14         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
15     }
16      
17     NSString *fontName = _listArray[indexPath.row];
18     cell.textLabel.text = fontName;
19     cell.textLabel.textColor = [UIColor blueColor];
20     cell.textLabel.font = [UIFont fontWithName:fontName size:12];
21      
22     return cell;
23 }

01     // 设置表视图的颜色
02    _tableView.backgroundColor = [UIColor yellowColor];
03
04     // 设置表视图的分割线的颜色
05 //    _tableView.separatorColor = [UIColor purpleColor];
06
07     // 设置表视图的分割线的风格
08  _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
09
10     // 设置表视图的头部视图(headView 添加子视图)
11 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
12  headerView.backgroundColor = [UIColor redColor]
13
14
15     // 添加子视图
16     UILabel *headText = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 80)];
17
18     headText.text = @"天晴朗,天晴朗天晴朗天晴朗!";
19     // 是否自动换行 0 换行,1 自动缩略
20     headText.numberOfLines = 0;
21
22     [headerView addSubview:headText];
23
24     [headText release];
25
26      
27     _tableView.tableHeaderView = headerView;
28
29     [headerView release];
30
31      
32     // 设置表视图的尾部视图(footerView 添加子视图)   
33
34     UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
35
36     footerView.backgroundColor = [UIColor yellowColor];
37
38     _tableView.tableFooterView = footerView;
39
40     [footerView release];

转http://my.oschina.net/wangdk/blog/150291
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值