34.tableView的编辑
来源:互联网 发布:关于室内设计的软件 编辑:程序博客网 时间:2024/05/16 05:14
1.基本概念
- tableView的编辑:cell的添加、删除。使用场景:删除一个下载好的视频,删除联系人;插⼊一条新的聊天记录等
- 编辑的步骤
1) 让tableView处于编辑状态-TableView方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
2) 指定tableView哪些行可以编辑-TableView DataSource方法- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath;
3) 指定tableView编辑的样式(添加、删除)-TableView Delegate方法- (UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath;
4) 编辑完成(先操作数据源,再修改UI)-TableView DataSource方法- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; - 移动的步骤
1) 让tableView处于编辑状态-TableView方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
2) 指定tableView哪些行可以移动-TableView DataSource⽅方法- (BOOL)tableView:(UITableView )tableView canMoveRowAtIndexPath:(NSIndexPath )indexPath;
3) 移动完成-TableView DataSource⽅方法- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath )destinationIndexPath
4) 监测移动过程,实现限制跨区移动-TableView方法- (NSIndexPath )tableView:(UITableView )tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath )sourceIndexPath toProposedIndexPath:(NSIndexPath )proposedDestinationIndexPath - UITableViewController继承自UIViewController,自带一个tableView self.view不是UIView⽽而是UITableView datasource和delegate默认都是self(UITableViewController) 开发中只需要建立UITableViewController子类,UITableViewController是封装好了各种delegate和datasource,能提高我们开发速度。
- 无论编辑还是移动,都先让tableView进入编辑状态,然后指定哪些行可以编辑或移动,编辑或者移动结束,要先修改数组或字典中的数据,再更改UI。
- 哪些行可以进行编辑(默认是YES)–>setEditing:animated:(默认是NO)–>哪些行可以进行移动(默认是YES)
2.简单运用
#import "MainViewController.h"@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)UITableView *tableView;@property(nonatomic,retain)NSMutableArray *arr;@end@implementation MainViewController- (instancetype)init{ self = [super init]; if (self) { self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil]; } return self;}- (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; self.navigationItem.rightBarButtonItem = self.editButtonItem; //直接打开tableview的可编辑模式(是否在左边显示一个"-"或"+"来进行编辑(YES显示))(默认是不显示) [self.tableView setEditing:YES animated:YES]; [self.tableView release];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.arr.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"mycell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = self.arr[indexPath.row]; return cell;}
#pragma mark 重写系统的编辑按钮点击触发的方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:YES]; [self.tableView setEditing:editing animated:YES];}#pragma mark 设置哪些行可以进行编辑(默认是可编辑的)- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ //奇数行可以编辑,偶不可变 if (indexPath.row % 2 == 0) { return NO; } return YES;}//[self.tableView setEditing:YES(1) animated:YES];//上面tableView:canEditRowAtIndexPath:打开代表1,否则代表0//1.(0,1)没有左边的"-"号,但是奇数行可以左划编辑删除//2.(1,0)所有行前都有"-",可以点击"-"删除,所有行也可以移动//3.(1,1)奇数行前面有"-"号,可以点击"-"删除,奇数行也可以移动//4.(0,0)没有左边的"-"号,但所有行是可以左划编辑删除//编辑的两种样式,插入和删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete;}#pragma mark 这个方法是iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮(覆盖掉原来的Delete),先添加的在右边--->- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { //按钮的点击索要触发的事件,都是写在blocl中 NSLog(@"触发了删除按钮"); }]; deleteAction.backgroundColor = [UIColor blueColor]; UITableViewRowAction *addAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"触发了置顶按钮"); }]; addAction.backgroundColor = [UIColor redColor]; return @[deleteAction,addAction];}//修改删除按钮的标题(默认是Delete)- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"来呀";}//删除数据- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { //先删除数据源 [self.arr removeObjectAtIndex:indexPath.row]; //通过重新加载数据来删除上面的cell //[self.tableView reloadData]; //也可以通过tableview来删除上面的cell,方法带有两个参数:哪个区的哪行,删除动画 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }}//设置哪些行可以进行移动(默认是可移动)- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;//默认是返回YES}//#pragma mark 移动----------------------->- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //1.先获取到起始的位置的数据(防止对象被释放掉要retain一次) NSString *str = [self.arr[sourceIndexPath.row] retain]; //2.把起始位置的对象从数据议案中移除 [self.arr removeObjectAtIndex:sourceIndexPath.row]; //3.把数据插入到数组的目的位置上去 [self.arr insertObject:str atIndex:destinationIndexPath.row]; [str release];}
简单截图
0 0
- 34.tableView的编辑
- 可编辑的TableView
- tableView的编辑模式
- tableView的编辑
- tableview的编辑
- tableView的编辑
- tableView的编辑
- TableView的编辑
- tableView的编辑和移动
- TableView编辑
- TableView编辑
- JAVAFX的TableView的单元格可编辑
- CYC- tableView的删除编辑移动
- 编辑删除收藏在tableView的内容
- CommonTableCollectView使用例子:tableView的编辑模式
- 自定义tableView编辑样式的图标
- ios TableView编辑状态多选框的修改
- TableView 编辑时,底部线的处理
- 结构体,联合体,内存分配
- Node.js详细解析
- hdu 5373 The shortest problem 2015多校联合训练赛#7 模拟
- 修改Tomcat内存大小
- 一小时内破解16位强密码
- 34.tableView的编辑
- 黑马程序员——C语言笔记之数组
- 交换机的原理及其配置(一)
- 杭电ACM2044java做法
- jquery动态添加、删除class样式
- 一般情况的三维旋转矩阵
- [BZOJ 1899] ZJOI 2004 Lunch 午餐 · 动态规划
- ”军火库“地址
- 集合1--List集合及泛型