UITableView基本用法

来源:互联网 发布:linux ubuntu安装教程 编辑:程序博客网 时间:2024/05/18 01:21
几乎大多数的IOS项目中都可以看得到UITableView 的影子,总结了一下,UITableView是iOS开发中,使用最广泛的组件之一,通常都用它来展示一列数据 。开始看一下UITableView的基本语法:


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

[cpp] view plaincopy
  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  


示例代码:

[cpp] view plaincopy
  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. }  

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

[cpp] view plaincopy
  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   //行缩进  

二、UITableViewDelegate的代理方法实现

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

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

[cpp] view plaincopy
  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. }  

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

[cpp] view plaincopy
  1. <span style="color:#3366ff;">//选中cell时的颜色</span>  
  2.   
  3. typedef enum {  
  4.     UITableViewCellSelectionStyleNone,  
  5.     UITableViewCellSelectionStyleBlue,  
  6.     UITableViewCellSelectionStyleGray  
  7. } UITableViewCellSelectionStyle  
TIPS:自定义选中cell的背景颜色

[cpp] view plaincopy
  1. <span style="font-size:18px;color:#ff0000;"> cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];  
  2.  cell.selectedBackgroundView.backgroundColor = [UIColor redColor];</span>  


[cpp] view plaincopy
  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  

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

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


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

0 0
原创粉丝点击