滑动删除指定行代码如下:

Controller.h文件

  1. #import <UIKit/UIKit.h>
  2. @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
  3. @property (nonatomic, strong) UITableView *myTableView;
  4. @property(nonatomic,strong) NSMutableArray *arrayOfRows;
  5. @end

Controller.m文件

  1. //
  2. //  TableViewController.m
  3. //  UITableViewDemo
  4. //
  5. //  Created by WildCat on 13-8-6.
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.
  7. //
  8. #import "TableViewController.h"
  9. @interface TableViewController ()
  10. @end
  11. @implementation TableViewController
  12. @synthesize myTableView;
  13. @synthesize arrayOfRows;
  14. - (void)viewDidLoad
  15. {
  16. [super viewDidLoad];
  17. // Do any additional setup after loading the view, typically from a nib.
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  20. //设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式
  21. self.myTableView.delegate = self;//设置代理为自身
  22. self.myTableView.dataSource = self;//设置数据源为自身
  23. self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  24. //确保TablView能够正确的调整大小
  25. arrayOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d", nil];//初始化表格数据
  26. [self.view addSubview:myTableView];
  27. }
  28. //设置每行的高度
  29. -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  30. CGFloat result = 20.0f;
  31. if ([tableView isEqual:self.myTableView]) {
  32. result = 80.0f;
  33. }
  34. return result;
  35. }
  36. //允许数据源告知必须加载到Table View中的表的Section数。
  37. //-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  38. //    NSInteger result = 0;
  39. //    if([tableView isEqual:myTableView]){
  40. //        result = 3;//一共三个section
  41. //    }
  42. //    return result;
  43. //}
  44. //设置每个Section呈现多少行
  45. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  46. return [self.arrayOfRows count];
  47. }
  48. //每行对应的数据
  49. -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  50. UITableViewCell *result = nil;
  51. if ([tableView isEqual:myTableView]) {
  52. static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识
  53. result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象
  54. if (result == nil) {
  55. result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。
  56. }
  57. //indexPath.section 表示section的索引 indexPath.row表示行数的索引
  58. result.textLabel.text = [self.arrayOfRows objectAtIndex:indexPath.row];
  59. }
  60. return result;
  61. }
  62. //点击某一行时候触发的事件
  63. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  64. if ([tableView isEqual:myTableView]) {
  65. NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);
  66. }
  67. }
  68. //要求委托方的编辑风格在表视图的一个特定的位置。
  69. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  70. UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格
  71. if ([tableView isEqual:myTableView]) {
  72. result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
  73. }
  74. return result;
  75. }
  76. -(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
  77. [super setEditing:editing animated:animated];
  78. [self.myTableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
  79. }
  80. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
  81. if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
  82. if (indexPath.row<[self.arrayOfRows count]) {
  83. [self.arrayOfRows removeObjectAtIndex:indexPath.row];//移除数据源的数据
  84. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
  85. }
  86. }
  87. }
  88. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  89. {
  90. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  91. if (self) {
  92. // Custom initialization
  93. }
  94. return self;
  95. }
  96. - (void)viewDidUnload
  97. {
  98. [super viewDidUnload];
  99. // Release any retained subviews of the main view.
  100. // self.myTableView = nil;
  101. }
  102. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  103. {
  104. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  105. }
  106. @end

执行截图:

转载请注明:

新浪微博:http://weibo.com/u/3202802157

IOS学习之路六(UITableView滑动删除指定行)的更多相关文章

  1. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  2. IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路

    在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...

  3. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

  4. sed 删除最后几行 和删除指定行 awk使用

    sed 删除最后几行 和删除指定行   转载原文链接:http://blog.51cto.com/lspgyy/1305489 sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的. 删 ...

  5. Pandas常用操作 - 删除指定行/指定列

    1. 删除指定行 new_df = df.drop(index='行索引') new_df = df.drop('行索引', axis='index') new_df = df.drop('行索引', ...

  6. 浅谈iOS学习之路(转)

    转眼学习iOS已经快两年的时间了,这个路上有挫折也有喜悦,一步步走过来发现这个过程是我这一辈子的财富,我以前的老大总是对我说,年轻就是最大的资本(本人91年),现在才算是慢慢的体会到,反观自己走过的这 ...

  7. 浅谈iOS学习之路

    转眼学习iOS已经快两年的时间了,这个路上有挫折也有喜悦,一步步走过来发现这个过程是我这一辈子的财富,我以前的老大总是对我说,年轻就是最大的资本(本人91年),现在才算是慢慢的体会到,反观自己走过的这 ...

  8. [Xcode 实际操作]五、使用表格-(6)UITableView滑动到指定单元格

    目录:[Swift]Xcode实际操作 本文将演示如何使表格滑动到指定的索引路径. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...

  9. REDHAT一总复习1 vim编辑器的使用 删除所有者列 删除指定行

    将文件/home/student/vimfile.txt 复制到server 上的/home/student/longlisting.txt . 根据下列要求,使用vim编辑器更改 /home/stu ...

随机推荐

  1. java_eclipse_svn 与服务器同步时 ,忽略某类型文件和文件夹

    1. 在项目开发中使用svn ,带来很大的方便,有时我们会把整个项目上传的svn服务器上 这样就包含了  编译过的class文件  以及 一些 .svn,.log文件,有些文件时本地complie 的 ...

  2. java_tomcat_the_APR based Apache Tomcat 小喵咪死活启动报错_临时方案

    报错信息如下: 信息: The APR based Apache Tomcat Native library which allows optimal performance in productio ...

  3. JS工具库之Lodash

    破狼 JavaScript工具库之Lodash 2015-04-11 16:08 by 破狼, 235 阅读, 2 评论, 收藏, 编辑 你还在为JavaScript中的数据转换.匹配.查找等烦恼吗? ...

  4. Oracle在rownum使用结果集排序

    Oracle在rownum使用结果集排序    对于 Oracle 的 rownum 问题,非常多资料都说不支持>,>=,=,between...and,仅仅能用以上符号(<.< ...

  5. tomcat加载类的顺序

    /bin:存放启动和关闭tomcat的脚本文件: /conf:存放tomcat的各种配置文件,比如:server.xml /server/lib:存放tomcat服务器所需要的各种jar文件(jar文 ...

  6. 发现新大陆:一个最简单的破解SSL加密网络数据包的方法

    1. 简介 相信能访问到这篇文章的同行基本上都会用过流行的网络抓包工具WireShark,用它来抓取相应的网络数据包来进行问题分析或者其他你懂的之类的事情. 一般来说,我们用WireShark来抓取包 ...

  7. js 正则之检测素数

    原文:js 正则之检测素数 相信很多人应该看过这篇文章,我第一次看到的时候是11年的样子,那时候学vbs的时候看过这个问题.原文<检查素数的正则表达式>,在文章里已经解释了他是怎么判断的, ...

  8. 探究Java中Map类

    Map以按键/数值对的形式存储数据,和数组非常相似,在数组中存在的索引,它们本身也是对象.       Map的接口       Map---实现Map       Map.Entry--Map的内部 ...

  9. 完整的thinphp+phpexcel实现excel报表的输出(有图有效果)

    准备工作:1.下载phpexcel1.7.6类包:2.解压至TP框架的ThinkPHP\Vendor目录下,改类包文件夹名为PHPExcel176,目录结构如下图:       编写代码(以一个订单汇 ...

  10. Android NDK进入发展

    使用互联网有很多javah命令生成一个头文件来完成JNI写,但事实上ADT集成NDK后.点点鼠标就可以了,网上的介绍是非常小懒的方法,在这里,我们主要谈论的懒惰JNI发展. 为ADT组态NDK.请个人 ...