UITableView使用方法

  1. -、建立 UITableView  
  2.  UITableView  *DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  
  3.  [DataTable setDelegate:self];  
  4.  [DataTable setDataSource:self];  
  5.  [self.view addSubview:DataTable];  
  6.  [DataTable release];  
  7.   
  8. 二、UITableView各Method说明  
  9.    
  10.   
  11. //Section总数  
  12. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  13.  return TitleData;  
  14. }  
  15.   
  16. // Section Titles  
  17. //每个section显示的标题  
  18. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  19.   return @"";  
  20. }  
  21.   
  22. //指定有多少个分区(Section),默认为1  
  23. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  24.   return 4;  
  25. }  
  26.   
  27. //指定每个分区中有多少行,默认为1  
  28. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  29. }  
  30.   
  31. //绘制Cell  
  32. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  33. static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
  34.     
  35.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:  
  36.                              SimpleTableIdentifier];  
  37. if (cell == nil) {    
  38.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
  39.                                        reuseIdentifier: SimpleTableIdentifier] autorelease];  
  40.  }  
  41.   cell.imageView.image=image;//未选cell时的图片  
  42.   cell.imageView.highlightedImage=highlightImage;//选中cell后的图片  
  43.   cell.text=//.....  
  44.   return cell;  
  45. }  
  46.   
  47. //行缩进  
  48. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
  49.   NSUInteger row = [indexPath row];  
  50.   return row;  
  51. }  
  52.   
  53. //改变行的高度  
  54. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  55.   return 40;  
  56. }  
  57.   
  58. //定位  
  59. [TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];  
  60.   
  61. //返回当前所选cell  
  62. NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];  
  63. [TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];  
  64.   
  65. [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];  
  66.   
  67. //选中Cell响应事件  
  68. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  69.  [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
  70. }  
  71.   
  72. //判断选中的行(阻止选中第一行)  
  73. -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  74. {  
  75.   NSUInteger row = [indexPath row];  
  76. if (row == 0)  
  77.          return nil;  
  78.      
  79. return indexPath;  
  80. }  
  81.   
  82. //划动cell是否出现del按钮  
  83. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {  
  84. }  
  85.   
  86. //编辑状态  
  87. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  88. forRowAtIndexPath:(NSIndexPath *)indexPath  
  89. {  
  90. }  
  91.   
  92. [topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];  
  93. //右侧添加一个索引表  
  94. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  95. }  
  96. //返回Section标题内容  
  97. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  98. }  
  99. //自定义划动时del按钮内容  
  100. - (NSString *)tableView:(UITableView *)tableView  
  101. titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath  
  102.   
  103. //跳到指的row or section  
  104. [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];  
  105.    
  106. 三、在UITableViewCell上建立UILable多行显示  
  107. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  108.     static NSString *CellIdentifier = @"Cell";     
  109.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  110.     if (cell == nil) {  
  111.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];  
  112.   UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];  
  113.   [Datalabel setTag:100];  
  114.   Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
  115.   [cell.contentView addSubview:Datalabel];  
  116.   [Datalabel release];  
  117.  }   
  118.  UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];  
  119.  [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];  
  120.  Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];  
  121.  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  122.     return cell;  
  123. }  
  124.    
  125.    
  126. //选中cell时的颜色  
  127. typedef enum {  
  128.     UITableViewCellSelectionStyleNone,  
  129.     UITableViewCellSelectionStyleBlue,  
  130.     UITableViewCellSelectionStyleGray  
  131. } UITableViewCellSelectionStyle  
  132.    
  133. //cell右边按钮格式  
  134. typedef enum {  
  135.     UITableViewCellAccessoryNone,                   // don't show any accessory view  
  136.     UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track  
  137.     UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks  
  138.     UITableViewCellAccessoryCheckmark               // checkmark. doesn't track  
  139. } UITableViewCellAccessoryType  
  140.    
  141. //是否加换行线  
  142. typedef enum {  
  143.     UITableViewCellSeparatorStyleNone,  
  144.     UITableViewCellSeparatorStyleSingleLine  
  145. } UITableViewCellSeparatorStyle  
  146.    
  147. //改变换行线颜色  
  148. tableView.separatorColor = [UIColor blueColor];  

你可能感兴趣的:(UITableView使用方法)