UITableView详解

来源:互联网 发布:福州seo技巧培训 编辑:程序博客网 时间:2024/06/07 04:03

一、iOS中创建UITableView控件
1、通过代码创建

[java] view plaincopy
  1. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460)];  
  2. [tableView setDelegate:self];  
  3. [tableView setDataSource:self];  
  4. [slef.view addSubView:tableView];  
2、xib中直接拖动控件添加


二、UITableView中的各种Method
//获取Section title的数组
[java] view plaincopy
  1. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  2.   return titleData;  
  3. }  

//指定每个section的标题(title)
[java] view plaincopy
  1. -(NSString *)tableView:(UITableView *)tableView titleForHanderInSection:(NSInteger)section{  
  2.   return @"sectionTitle";  
  3. }  

//指定有多少个分区(Section),默认值为1
[java] view plaincopy
  1. -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{  
  2.   return number;  
  3. }  

//指定每个分区(Section)有多少行,默认值为1
[java] view plaincopy
  1. -(NSInteger)tableView:(NSTableView *)tableView numbleOfRowsInSection:(NSInteger)section{  
  2.   return rowsCount;  
  3. }  

//绘制Cell(学android的同学应该知道,这个类似于android adapter的中getView,用于绘制每个item)

[java] view plaincopy
  1. -(NSTableViewCell *)tableView:(NSTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.   static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
  3.   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];  
  4.   if(cell==nil){  
  5.     cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier];  
  6.   }  
  7.   cell.imageView.image=image;//未选cell时的图片  
  8.   cell.imageView.highlightedImage=highlightImage;//选中cell后的图片  
  9.     cell.text=//.....  
  10.     return cell;  
  11. }  

//行缩进
[java] view plaincopy
  1. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.   NSUInteger row = [indexPath row];  
  3.   return row;  
  4. }  

//改变行的高度
[java] view plaincopy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.   return 40;  
  3. }  

//定位
[java] view plaincopy
  1. [TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];  

//返回当前所选cell

[java] view plaincopy
  1. NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];  
  2. [TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];  
  3.   
  4.   
  5. [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];  

//选中Cell响应事件
[java] view plaincopy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.   [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
  3. }  

//判断选中的行(阻止选中第一行)
[java] view plaincopy
  1. -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSUInteger row = [indexPath row];  
  4.     if (row == 0)  
  5.         return nil;     
  6.     return indexPath;  
  7. }  

//划动cell是否出现del按钮
[java] view plaincopy
  1. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {  
  2. }  

[java] view plaincopy
  1. //编辑状态  
  2. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  3. forRowAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5. }  
  6. [topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];  
  7. //右侧添加一个索引表  
  8. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  9. }  
  10. //返回Section标题内容  
  11. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  12. }  
  13. //自定义划动时del按钮内容  
  14. - (NSString *)tableView:(UITableView *)tableView  
  15. titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath  
  16. //跳到指的row or section  
  17. [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

1 0