【转】iOS UITableView的方法解析

原文网址:http://www.cnblogs.com/wfwenchao/articles/3718742.html

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //初始化数据  
  5.     NSArray *array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];  
  6.     NSArray *array2_=@[@"李小龙",@"李小路"];  
  7.     NSArray *array3_=@[@"王刚"];  
  8.     self.myDic=@{@"老张家":array1_, @"老李家":array2_, @"老王家":array3_};  
  9.   
  10.       
  11.     UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];  
  12.     myTableView_.delegate=self;  
  13.     myTableView_.dataSource=self;  
  14.     //改变换行线颜色lyttzx.com  
  15.     myTableView_.separatorColor = [UIColor blueColor];  
  16.     //设定Header的高度,  
  17.     myTableView_.sectionHeaderHeight=50;  
  18.     //设定footer的高度,  
  19.     myTableView_.sectionFooterHeight=100;  
  20.     //设定行高  
  21.     myTableView_.rowHeight=100;  
  22.     //设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine  
  23.     [myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];  
  24.     //设定cell分行线颜色  
  25.     [myTableView_ setSeparatorColor:[UIColor redColor]];  
  26.     //编辑tableView  
  27.     myTableView_.editing=NO;  
  28.   
  29.     [self.view addSubview:myTableView_];  
  30.       
  31.     //跳到指的row or section  
  32.     [myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:2]  
  33.                      atScrollPosition:UITableViewScrollPositionBottom animated:NO];  
  34.   
  35. }  
  36.   
  37.   
  38.   
  39. //指定有多少个分区(Section),默认为1  
  40. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  41.     return [[self.myDic allKeys] count];  
  42. }  
  43.   
  44.   
  45. //每个section底部标题高度(实现这个代理方法后前面 sectionHeaderHeight 设定的高度无效)  
  46. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
  47.     return 20;  
  48. }  
  49.   
  50. //每个section头部标题高度(实现这个代理方法后前面 sectionFooterHeight 设定的高度无效)  
  51. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{  
  52.     return 20;  
  53. }  
  54.   
  55. //每个section头部的标题-Header  
  56. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  57.     return [[self.myDic allKeys] objectAtIndex:section];  
  58. }  
  59.   
  60. //每个section底部的标题-Footer  
  61. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{  
  62.     return nil;  
  63. }  
  64.   
  65. //用以定制自定义的section头部视图-Header  
  66. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{  
  67.     return nil;  
  68. }  
  69.   
  70. //用以定制自定义的section底部视图-Footer  
  71. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{  
  72.     UIImageView *imageView_=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];  
  73.     imageView_.image=[UIImage imageNamed:@"1000.png"];  
  74.     return [imageView_ autorelease];  
  75. }  
  76.   
  77.   
  78. //指定每个分区中有多少行,默认为1  
  79. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  80.     return [[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:section]] count];  
  81. }  
  82.   
  83. //改变行的高度(实现主个代理方法后 rowHeight 设定的高度无效)  
  84. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  85.     return 100;  
  86. }  
  87.   
  88.   
  89. //绘制Cell  
  90. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  91.     static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
  92.       
  93.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:  
  94.                              SimpleTableIdentifier];  
  95.       
  96.     if (cell == nil) {  
  97.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
  98.                                        reuseIdentifier: SimpleTableIdentifier] autorelease];  
  99.      //设定附加视图  
  100.         [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];  
  101. //        UITableViewCellAccessoryNone,                   // 没有标示  
  102. //        UITableViewCellAccessoryDisclosureIndicator,    // 下一层标示  
  103. //        UITableViewCellAccessoryDetailDisclosureButton, // 详情button  
  104. //        UITableViewCellAccessoryCheckmark               // 勾选标记  
  105.           
  106.      //设定选中cell时的cell的背影颜色  
  107.         cell.selectionStyle=UITableViewCellSelectionStyleBlue;     //选中时蓝色效果  
  108. //        cell.selectionStyle=UITableViewCellSelectionStyleNone; //选中时没有颜色效果  
  109. //        cell.selectionStyle=UITableViewCellSelectionStyleGray;  //选中时灰色效果  
  110. //          
  111. //        //自定义选中cell时的背景颜色  
  112. //        UIView *selectedView = [[UIView alloc] initWithFrame:cell.contentView.frame];  
  113. //        selectedView.backgroundColor = [UIColor orangeColor];  
  114. //        cell.selectedBackgroundView = selectedView;  
  115. //        [selectedView release];  
  116.   
  117.           
  118. //        cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被选中  
  119.   
  120.     }  
  121.       
  122.     //这是设置没选中之前的背景颜色  
  123.     cell.contentView.backgroundColor = [UIColor clearColor];  
  124.     cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未选cell时的图片  
  125.     cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//选中cell后的图片  
  126.     cell.textLabel.text=[[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];  
  127.     return cell;  
  128. }  
  129.   
  130.   
  131. //行缩进  
  132. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
  133.     NSUInteger row = [indexPath row];  
  134.     return row;  
  135. }  
  136.   
  137. //选中Cell响应事件  
  138. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  139.     [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
  140.       
  141.     //得到当前选中的cell  
  142.     UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];  
  143.     NSLog(@"cell=%@",cell);  
  144. }  
  145.   
  146. //行将显示的时候调用,预加载行  
  147. -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  148. {  
  149.     NSLog(@"将要显示的行是\n cell=%@  \n indexpath=%@",cell,indexPath);  
  150. }  
  151.   
  152. //选中之前执行,判断选中的行(阻止选中第一行)  
  153. -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  154. {  
  155.     NSUInteger row = [indexPath row];  
  156.     if (row == 0)  
  157.         return nil;  
  158.     return indexPath;  
  159. }  
  160.   
  161.   
  162.   
  163. //编辑状态,点击删除时调用  
  164. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  165. forRowAtIndexPath:(NSIndexPath *)indexPath  
  166. {  
  167.       
  168. }  
  169.   
  170. //cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法  
  171. -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{  
  172.     NSLog(@"当前点击的详情button \n indexpath=%@",indexPath);  
  173. }  
  174.   
  175. //右侧添加一个索引表  
  176. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  177.     return [self.myDic allKeys];  
  178. }  
  179.   
  180. //划动cell是否出现del按钮  
  181. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {  
  182.     return YES;  
  183. }  
  184.   
  185. //设定横向滑动时是否出现删除按扭,(阻止第一行出现)  
  186. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  187. {  
  188.     if (indexPath.row==0) {  
  189.         return UITableViewCellEditingStyleNone;  
  190.     }  
  191.     else{  
  192.         return UITableViewCellEditingStyleDelete;  
  193.     }  
  194.     return UITableViewCellEditingStyleDelete;  
  195. }  
  196.   
  197. //自定义划动时delete按钮内容  
  198. - (NSString *)tableView:(UITableView *)tableView  
  199. titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{  
  200.     return @"删除这行";  
  201.           
  202. }  
  203.   
  204. //开始移动row时执行  
  205. -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath  
  206. {  
  207.     NSLog(@"sourceIndexPath=%@",sourceIndexPath);  
  208.     NSLog(@"sourceIndexPath=%@",destinationIndexPath);  
  209. }  
  210.   
  211. //滑动可以编辑时执行  
  212. -(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath  
  213. {  
  214.     NSLog(@"willBeginEditingRowAtIndexPath");  
  215. }  
  216.   
  217. //将取消选中时执行, 也就是上次先中的行  
  218. -(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath  
  219. {  
  220.     NSLog(@"上次选中的行是  \n indexpath=%@",indexPath);  
  221.     return indexPath;  
  222. }  
  223.   
  224.   
  225. //让行可以移动  
  226. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  
  227. {  
  228.     return NO;  
  229. }  
  230.   
  231. //移动row时执行  
  232. -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath  
  233. {  
  234.     NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");  
  235.     //用于限制只在当前section下面才可以移动  
  236.     if(sourceIndexPath.section != proposedDestinationIndexPath.section){  
  237.         return sourceIndexPath;  
  238.     }  
  239.     return proposedDestinationIndexPath;  
  240. }  

 

原文连接:http://sgm881218.iteye.com/blog/1852325

posted on 2016-06-23 16:21  wi100sh  阅读(193)  评论(0编辑  收藏  举报

导航