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

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

【转】iOS UITableView的方法解析的更多相关文章

  1. IOS UITableview代理方法总结

    tableview的datasource代理 @required的两个数据源方法 1.返回每个 session 中 cell 的个数 - (NSInteger)tableView:(UITableVi ...

  2. iOS开发init方法解析

    自定义的init方法,都必须调用父类的init方法. 一般情况下为: - (id)init {      [super init];      xxx = xxx; }   通常情况下,这种模式可以满 ...

  3. iOS 详解NSXMLParser方法解析XML数据方法

    前一篇文章已经介绍了如何通过URL从网络上获取xml数据.下面介绍如何将获取到的数据进行解析. 下面先看看xml的数据格式吧! <?xml version="1.0" enc ...

  4. [转载]iOS 10 UserNotifications 框架解析

    活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...

  5. IOS的XML文件解析,利用了NSData和NSFileHandle

    如果需要了解关于文档对象模型和XML的介绍,参看 http://www.cnblogs.com/xinchrome/p/4890723.html 读取XML 上代码: NSFileHandle *fi ...

  6. IOS UITableView NSIndexPath属性讲解

    IOS UITableView NSIndexPath属性讲解   查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...

  7. 四种方法解析JSON数据

    (1)使用TouchJSon解析方法:(需导入包:#import "TouchJson/JSON/CJSONDeserializer.h") //使用TouchJson来解析北京的 ...

  8. iOS学习——JSON数据解析(十一)

    在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...

  9. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

随机推荐

  1. Proxmox虚拟机增加硬盘容量

    1.首先在虚拟机控制台选择调整硬盘容量,弹出窗口为增加的容量 2.重启虚拟机,用fdisk –l查看新增容量是否被识别 3.用cfdisk创建分区,分区格式为Primary 8e (Linux LVM ...

  2. Chp11: Sorting and Searching

    Common Sorting Algo: Bubble Sort: Runime: O(n2) average and worst case. Memory: O(1). void BubbleSor ...

  3. Android开发--解决AndroidADT开发工具不能代码提示的问题

    google android的新的开发工具,打开以后没有代码自动提示功能,下面对ADT工具的一些配置: 1.设置代码的字体 设置JAVA文件代码的字体:我这里设置的14 常规.

  4. POJ2503Babelfish

    http://poj.org/problem?id=2503 这个题一开始是想用字典树,发现太麻烦..... #include<cstdio> #include<cstring> ...

  5. 李洪强iOS开发之最全App上架流程

    在上架App之前想要 真机测试的同学 请查看 iOS- 最全的真机测试教程 里面包含怎么让多台电脑同时 上架App和同时真机调试.P12文件的使用详解 准备 开发者账号 完工的项目 上架步骤 一.创建 ...

  6. 用JUnit4进行单元测试

    转载:http://tonl.iteye.com/blog/1948869 参考: http://thihy.iteye.com/blog/1771826 http://developer.51cto ...

  7. 入门视频采集与处理(BT656简介)

    入门视频采集与处理(BT656简介) http://ticktick.blog.51cto.com/823160/553535 1.  帧的概念(Frame) 一个视频序列是由N个帧组成的,采集图像的 ...

  8. 移植 FFMPEG-2.2.4 -(编译)

    源码下载:http://www.ffmpeg.org/download.html编译安装: http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu su ...

  9. 英特尔Intel

    公司名称 英特尔(集成电路公司)Intel Corporation(Integrated Electronics Corporation) 英特尔公司是全球最大的半导体芯片制造商,它成立于1968年, ...

  10. CString, QString, char*之间的转换(包括VC编译开关)

    传给未分配内存的const char* (LPCTSTR)指针. CString cstr(asdd); const char* ch = (LPCTSTR)cstr; ch指向的地址和cstr相同. ...