tableView的编辑模式

表视图可以进入编辑模式,当进入编辑模式就可以进行删除、插入、移动单元等操作

 效果图:

  

让表视图进入编辑模式,进入编辑模式的方法有两种,一种是使用导航栏的edit

按钮,另一种是设置tableView的editing属性进入编辑模式。

最后通过实现UITableViewDataSource协议的方法实现单元格的删除、插入和移动

1,在viewDidLoad方法里面指定导航栏的右按钮为edit按钮

self.navigationItem.rightBarButtonItem =self.editButtonItem;

2,另一种进入编辑模式的方式是修改tableView的editing属性

该属性是一个BOOL类型,默认值是NO,这里给导航栏添加一个左按钮,通过点击左按钮修改editing属性的值

进入和退出编辑模式

- (void)editTableView:(UIBarButtonItem *)button

{

  //修改editing属性的值,进入或退出编辑模式

[self.tableView setEditing:!self.tableView.editinganimated:YES];

  if(self.tableView.editing){

   button.title = @"完成";

  }

  else{

  button.title = @"编辑";

}

}

实现删除和插入行

  两方法一响应

方法一:那些行进入编辑模式

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

方法二:进入编辑模式的cell是删除还是增加

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewCellEditingStyleDelete;

}

注意:这个方法里一般返回两种类型,还有一中默认类型

删除:UITableViewCellEditingStyleDelete

增加:UITableViewCellEditingStyleInsert

响应:点击当点击delete后执行的删除过程

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

  //删除数据源里的数据

  [self.array removeObjectAtIndex:indexPath.row];

  //再删除tableView中对应的行

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

注意:先除数据源里的数据,删除tableView中对应的行

实现移动行

 当tableView进入编辑模式之后,默认每一行都是可以移动,每一行尾部有一个图标

  为三行灰色横线的按钮,就是表示该行可以移动

一方法一响应

方法一:那些cell可以移动

-(BOOL)tableView:(UITableView *)tableView

canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

响应:移动的具体操作

- (void)tableView:(UITableView *)tableView

moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

toIndexPath:(NSIndexPath *)toIndexPath

{

TRStudent *stu=self.array[fromIndexPath.row];

[self.array removeObjectAtIndex:fromIndexPath.row];

[self.array insertObject:stuatIndex:toIndexPath.row];

}

案例

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"

#import"TRStudent.h"

#import "TRTableViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

TRTableViewController *tr1=[[TRTableViewController alloc] initWithNibName:@"TRTableViewController" bundle:nil];

UINavigationController *navi=[[UINavigationController alloc] initWithRootViewController:tr1];

self.window.rootViewController=navi;

[self.window makeKeyAndVisible];

return YES;

}

@end

TRTableViewController.h

#import <UIKit/UIKit.h>

@interface TRTableViewController : UITableViewController

@property(nonatomic,strong) NSMutableArray *array;

@end

TRTableViewController.m

#import "TRTableViewController.h"

#import "TRStudent.h"

@interface TRTableViewController ()

@end

@implementation TRTableViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

return self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

}

- (void)viewDidLoad

{

[super viewDidLoad];

self.array=[TRStudent getarray];

self.navigationItem.rightBarButtonItem=self.editButtonItem;

}

//有多少分区

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

//有多少个cell单元

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return self.array.count;

}

//cell单元

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if(cell==nil)

{

cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

}

TRStudent *stu=self.array[indexPath.row];

cell.textLabel.text=stu.name;

return cell;

}

//那些行进入编辑模式,根据你的需求自行设置,我这里设置的全部

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

//进入编辑模式的cell是删除还是增加

//自行设置,我这里设置的是最后一个cell单元是增加,其他是删除

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView

editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

if(indexPath.row==self.array.count-1)

return UITableViewCellEditingStyleInsert;

return UITableViewCellEditingStyleDelete;

}

//点击当点击delete后执行的删除增加过程

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

[self.array removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

} else if (editingStyle == UITableViewCellEditingStyleInsert) {

TRStudent *str2=[[TRStudent alloc] init];

str2.name=@"qwer";

str2.name=@"124456";

[self.array addObject:str2];

NSIndexPath *insetindexpath=[NSIndexPath indexPathForRow:self.array.count-1 inSection:0];

[tableView insertRowsAtIndexPaths:@[insetindexpath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

//那些cell可移动

-(BOOL)tableView:(UITableView *)tableView

canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

//移动

- (void)tableView:(UITableView *)tableView

moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

toIndexPath:(NSIndexPath *)toIndexPath

{

TRStudent *stu=self.array[fromIndexPath.row];

[self.array removeObjectAtIndex:fromIndexPath.row];

[self.array insertObject:stuatIndex:toIndexPath.row];

}

@end

TRStudent.h

#import <Foundation/Foundation.h>

@interface TRStudent : NSObject

@property(nonatomic,strong) NSString *name;

@property(nonatomic,strong) NSString *phone;

+(NSMutableArray *)getarray;

@end

TRStudent.m

#import "TRStudent.h"

@implementation TRStudent

+(NSMutableArray *)getarray

{

TRStudent *stu1=[[TRStudent alloc] init];

stu1.name=@"q";

stu1.phone=@"12345";

TRStudent *stu2=[[TRStudent alloc] init];

stu2.name=@"qqw";

stu2.phone=@"12345";

TRStudent *stu3=[[TRStudent alloc] init];

stu3.name=@"sdsq";

stu3.phone=@"12345";

NSMutableArray *mut=[[NSMutableArray alloc] init];

[mut addObject:stu1];

[mut addObject:stu2];

[mut addObject:stu3];

return mut;

}

@end

ios之UITableViewController(二) tableView的编辑模式的更多相关文章

  1. iOS UIKit:TableView之编辑模式(3)

    一般table view有编辑模式和正常模式,当table view进入编辑模式时,会在row的左边显示编辑和重排控件,如图 42所示的编辑模式时的控件布局:左边的editing control有表 ...

  2. 07-UIKit(tableview的编辑模式、accessoryView)

    目录: 一.tableview的编辑模式-增删改查 二.不使用继承创建tableview 三.accessoryView辅助视图 回到顶部 一.tableview的编辑模式-增删改查 [1-conta ...

  3. IOS开发学习笔记032-UITableView 的编辑模式

    UITableView 的三种编辑模式 1.删除 2.排序 3.添加 进入编辑模式,需要设置一个参数 - (IBAction)remove:(UIBarButtonItem *)sender { NS ...

  4. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  5. ios 继承UITableViewController,更改tableview样式

    // 继承UITableViewController,更改tableview样式 - (instancetype)initWithStyle:(UITableViewStyle)style { ret ...

  6. iOS开发UI篇-tableView在编辑状态下的批量操作(多选)

    先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @pr ...

  7. iOS 中UITableViewController 中tableView 会被状态栏覆盖的问题

    解决办法在 生命周期函数viewDidAppear中设置即可 - (void)viewDidAppear:(BOOL)animated { self.tableView.frame = CGRectM ...

  8. IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )

    *****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...

  9. UITableView编辑模式大全解

    1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...

随机推荐

  1. Hadoop: The Definitive Guide (3rd Edition)

    chapter 1 解决计算能力不足的问题,不是去制造更大的计算机,而是用更多的计算机来解决问题. 我们生活在一个数据的时代.“大数据”的到来不仅仅是影响到那些科研和金融机构,对小型企业以及我们个人都 ...

  2. Android游戏背景音乐音效音量控制

    游戏音效就是我们在玩游戏时出现的音乐,这个也是每个游戏必备的一部分,但有是你做游戏的背景音乐有间断的感觉的话,我们可以用 getCurrentPosition()这个方法来判断一下声音播放的偏移.其实 ...

  3. 【Java基础】Java面试题目整理与解说(二)

    1.Collection 和 Collections 的差别. Collection 是集合类的上级接口,继承于他的接口主要有 Set 和 List. Collections 是针对集合类的一个帮助类 ...

  4. iOS开发——动画编程Swift篇&(四)CABasicAnimation动画

    CABasicAnimation动画 //CABasicAnimation-不透明度 @IBAction func cabOpacity() { let animation = CABasicAnim ...

  5. oc-16-set,get方法

    S.h #import <Foundation/Foundation.h> /** 解决方案: 1.不用@public修饰 2.我们对象有访问和设置成员变量的两种操作 1>设置值 p ...

  6. WSS3.0 SDK中的不同列表类型的对应值

    Value Description 100 Generic list 101 Document library 102 Survey 103 Links list 104 Announcements ...

  7. close和shutdown函数的区别

    close函数首先是将传入的socket句柄引用数减1(因为fork进程时会导致socket句柄被多个进程引用),待到引用数等于0的时候,close才会真正关闭连接. shutdown函数是立刻关闭连 ...

  8. 解决faststone capture在台式机上录制屏幕视频没有声音的问题

    点击屏幕录像机: 在弹出的屏幕录像机窗口中选择录制音频,然后点击选项按钮: 在弹出的选项按钮中选择视频选项,将所有的多选框都选中,注:最后一个选项录制windows XP的工具提示最好也选上,我发现如 ...

  9. VS 之 InstallShield Limited Edition for Visual Studio 2015 图文教程

    从Visual Studio 2012开始,微软就把自家原来的安装与部署工具彻底废掉了,转而让大家去安装使用第三方的打包工具“InstallShield Limited Edition for Vis ...

  10. Linux 下configure 参数配置与软件的安装与卸载

    Linux环境下的软件安装,并不是一件容易的事情:如果通过源代码编译后在安装,当然事情就更为复杂一些:现在安装各种软件的教程都非常普遍:但万变不离其中,对基础知识的扎实掌握,安装各种软件的问题就迎刃而 ...