[转]UITableView的基本用法

几乎大多数的IOS项目中都可以看得到UITableView 的影子,总结了一下,UITableView是iOS开发中,使用最广泛的组件之一,通常都用它来展示一列数据 。开始看一下UITableView的基本语法:


一、UITableView有两个代理协议
Protocol UITableViewDataSource:用来给TableView提供数据
Protocal UITableViewDelegate:控制TableView的展示方式以及事件响应
二、实现代理方法
 1、UITableViewDataSource代理方法实现

 

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //指定有多少个分区(Section),默认为1   
  2. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//指定每个分区中有多少行,默认为1   
  3. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //绘制Cell  
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //指定有多少个分区(Section),默认为1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//指定每个分区中有多少行,默认为1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //绘制Cell

 

 

示例代码:

 

  1.  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  2.     return1;//这里使用默认,如果你的数据需要分组显示,在这里就可以定义你所需要的组的个数   
  3. }  
  4.    
  5. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   
  6.    return [arrays count];//arrays是你所定义的数据存储数组   
  7. }  
  8.    
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  10.     static NSString *MyIdentifier = @"MyIdentifier"//相当于一个行标识   
  11.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];  
  12.     //tableViewCell重绘   
  13.     if (cell == nil) {  
  14.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;  
  15.     }  
  16.     NSUInteger row = indexPath.row; //获取行号   
  17.     NSString  *titleStr = [arrays objectAtIndex:row];//获取数据   
  18.     cell.textLabel.text = titleStr;//数据显示   
  19.     return cell;  
  20.    
  21. }  
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return1;//这里使用默认,如果你的数据需要分组显示,在这里就可以定义你所需要的组的个数
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
   return [arrays count];//arrays是你所定义的数据存储数组
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier"; //相当于一个行标识
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	//tableViewCell重绘
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
    }
	NSUInteger row = indexPath.row; //获取行号
    NSString  *titleStr = [arrays objectAtIndex:row];//获取数据
    cell.textLabel.text = titleStr;//数据显示
    return cell;
 
}

当然这里还有一些复杂的使用,例如headerView 、 footerView  、titleForHeaderInSection  等等 。

 

 

  1.  -(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section //设置分区高度   
  2.   
  3. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath    //改变行的高度   
  4.   
  5. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath   //行缩进  
 -(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section //设置分区高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath    //改变行的高度

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath   //行缩进

二、UITableViewDelegate的代理方法实现

 

UITableViewDelegate用来管理Row的选择和编辑,有四个方法如下:

  1. tableView:willSelectRowAtIndexPath:  
  2. tableView:didSelectRowAtIndexPath:  
  3. tableView:willDeselectRowAtIndexPath:  
  4. tableView:didDeselectRowAtIndexPath:  
tableView:willSelectRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
tableView:willDeselectRowAtIndexPath:
tableView:didDeselectRowAtIndexPath:
此四个方法管理Row的选择. 例如willSelectRowAtIndexPath, 如果此方法返回nil,那么所属的row将无法被选中。
  1. tableView:willBeginEditingRowAtIndexPath:  
  2. tableView:didEndEditingRowAtIndexPath:  
  3. tableView:editingStyleForRowAtIndexPath:  
  4. tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:  
tableView:willBeginEditingRowAtIndexPath:
tableView:didEndEditingRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath:
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
此四个方法在编辑Row时会被触发。editingStyleForRowAtIndexPath决定Row是否可以被编辑,删除或者移动。targetIndexPathForMoveFromRowAtIndexPath则在移动Row时会把触发,在交换Row位置的时候,必须同时交换DataSource中数据的位置。
示例代码:

 

 

  1. //行缩进   
  2. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row];   
  3.     return row;  
  4. }   
  5. //改变行的高度    
  6. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{   
  7.     return 40;   
  8. }  
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; 
	return row;
} 
//改变行的高度 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
	return 40; 
}

三、常用的一些枚举类型选择  

 

 

[cpp]
  1. <SPAN style="COLOR: #3366ff">//选中cell时的颜色</SPAN>   
  2.   
  3. typedef enum {  
  4.     UITableViewCellSelectionStyleNone,  
  5.     UITableViewCellSelectionStyleBlue,  
  6.     UITableViewCellSelectionStyleGray  
  7. } UITableViewCellSelectionStyle  
//选中cell时的颜色

typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
TIPS:自定义选中cell的背景颜色

 

 

[cpp]
  1. <SPAN style="COLOR: #ff0000; FONT-SIZE: 18px"> cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];  
  2.  cell.selectedBackgroundView.backgroundColor = [UIColor redColor];</SPAN>  
 cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
 cell.selectedBackgroundView.backgroundColor = [UIColor redColor];


[cpp]
  1. <SPAN style="COLOR: #3333ff">//cell右边按钮格式</SPAN>   
  2.   
  3. typedef enum {  
  4.   
  5.     UITableViewCellAccessoryNone,                   // don't show any accessory view没有附件   
  6.   
  7.     UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track 黑色向右的箭头   
  8.   
  9.     UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks  蓝色附件按钮   
  10.   
  11.     UITableViewCellAccessoryCheckmark               // checkmark. doesn't track  复选框,支持选择   
  12.   
  13. } UITableViewCellAccessoryType  
//cell右边按钮格式

typedef enum {

    UITableViewCellAccessoryNone,                   // don't show any accessory view没有附件

    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track 黑色向右的箭头

    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks  蓝色附件按钮

    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track  复选框,支持选择

} UITableViewCellAccessoryType

[cpp]
  1.  //是否加换行线    
  2.   
  3. typedef enum {  
  4.     UITableViewCellSeparatorStyleNone,  
  5.     UITableViewCellSeparatorStyleSingleLine  
  6. } UITableViewCellSeparatorStyle  
 //是否加换行线 

typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle

  1. //改变换行线颜色    
  2.   
  3. ableView.separatorColor = [UIColor blueColor];  
 //改变换行线颜色 

tableView.separatorColor = [UIColor blueColor];

 

 

  1. <SPAN style="FONT-SIZE: 18px"//系统提供的UITableView也包含了四种风格的布局    
  2. typedef enum {  
  3.     UITableViewCellStyleDefault,   
  4.     UITableViewCellStyleValue1,  
  5.     UITableViewCellStyleValue2,  
  6.     UITableViewCellStyleSubtitle  
  7. } UITableViewCellStyle;</SPAN>  

原文连接:http://blog.csdn.net/zyc851224/article/details/7879262

posted @ 2013-01-28 20:31  多多smile  阅读(192)  评论(0)    收藏  举报