iOS开发笔记--TableView 详细解释

来源:互联网 发布:小红书 有钱人 知乎 编辑:程序博客网 时间:2024/04/29 22:54

一、建立 UITableView

[cpp] view plain copy
  1. DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  
  2.  [DataTable setDelegate:self];  
  3.  [DataTable setDataSource:self];  
  4.  [self.view addSubview:DataTable];  
  5.  [DataTable release];  

二、UITableView各Method说明

[cpp] view plain copy
  1. //Section总数  
  2. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  3.  return TitleData;  
  4. }  
  5.    
  6. // Section Titles  
  7. //每个section显示的标题  
  8. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  9.  return @"";  
  10. }  
  11.    
  12. //指定有多少个分区(Section),默认为1  
  13. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  14.  return 4;  
  15. }  
  16.    
  17. //指定每个分区中有多少行,默认为1  
  18. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  19. }  
  20.    
  21. //绘制Cell  
  22. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  23. static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
  24.     
  25.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:  
  26.                              SimpleTableIdentifier];  
  27.     if (cell == nil) {    
  28.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
  29.                                        reuseIdentifier: SimpleTableIdentifier] autorelease];  
  30.  }  
  31.  cell.imageView.image=image;//未选cell时的图片  
  32.  cell.imageView.highlightedImage=highlightImage;//选中cell后的图片  
  33.  cell.text=//.....  
  34.  return cell;  
  35. }  
  36.    
  37. //行缩进  
  38. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
  39.  NSUInteger row = [indexPath row];  
  40.  return row;  
  41. }  
  42.    
  43. //改变行的高度  
  44. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  45.     return 40;  
  46. }  
  47.    
  48. //定位  
  49. [TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];  
  50.    
  51. //返回当前所选cell  
  52. NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];  
  53. [TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];  
  54.    
  55. [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];  
  56.    
  57. //选中Cell响应事件  
  58. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  59.  [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
  60. }  
  61.    
  62. //判断选中的行(阻止选中第一行)  
  63. -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  64. {  
  65.     NSUInteger row = [indexPath row];  
  66.     if (row == 0)  
  67.         return nil;  
  68.      
  69.     return indexPath;  
  70. }  
  71.    
  72. //划动cell是否出现del按钮  
  73. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {  
  74. }  
  75.    
  76. //编辑状态  
  77. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  78. forRowAtIndexPath:(NSIndexPath *)indexPath  
  79. {  
  80. }  
  81. [topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];  
  82. //右侧添加一个索引表  
  83. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  84. }  
  85. //返回Section标题内容  
  86. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  87. }  
  88. //自定义划动时del按钮内容  
  89. - (NSString *)tableView:(UITableView *)tableView  
  90. titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath  
  91. //跳到指的row or section  
  92. [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];  

三、在UITableViewCell上建立UILable多行显示

[cpp] view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.     static NSString *CellIdentifier = @"Cell";     
  3.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  4.     if (cell == nil) {  
  5.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];  
  6.   UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];  
  7.   [Datalabel setTag:100];  
  8.   Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
  9.   [cell.contentView addSubview:Datalabel];  
  10.   [Datalabel release];  
  11.  }   
  12.  UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];  
  13.  [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];  
  14.  Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];  
  15.  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  16.     return cell;  
  17. }  
  18. //选中cell时的颜色  
  19. typedef enum {  
  20.     UITableViewCellSelectionStyleNone,  
  21.     UITableViewCellSelectionStyleBlue,  
  22.     UITableViewCellSelectionStyleGray  
  23. } UITableViewCellSelectionStyle   
  24. //cell右边按钮格式  
  25. typedef enum {  
  26.     UITableViewCellAccessoryNone,                   // don't show any accessory view  
  27.     UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track  
  28.     UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks  
  29.     UITableViewCellAccessoryCheckmark               // checkmark. doesn't track  
  30. } UITableViewCellAccessoryType  
  31. //是否加换行线  
  32. typedef enum {  
  33.     UITableViewCellSeparatorStyleNone,  
  34.     UITableViewCellSeparatorStyleSingleLine  
  35. } UITableViewCellSeparatorStyle//改变换行线颜色  
  36. tableView.separatorColor = [UIColor blueColor];  
0 0
原创粉丝点击