编辑表格@tableView实现增加、删除、移动的操作
2016-01-13 15:26
916 查看
首先做好准备工作,在storyboard中做好工具条和tableView的准备工作,然后进行以下操作。。。看代码可知具体操作:
//=========================================
//要想实现增加删除操作,必须做到这三部,
//1、self.table.editing 即编辑状态要打开。为YES
//2、实现方法:-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//<即返回单元格的编辑状态>
//3、实现方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//<即进行删除增加操作>
//=========================================
上方图片1
//=========================================
//要想实现增加删除操作,必须做到这三部,
//1、self.table.editing 即编辑状态要打开。为YES
//2、实现方法:-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//<即返回单元格的编辑状态>
//3、实现方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//<即进行删除增加操作>
//=========================================
//============================= //实现移动的操作只需两步 //1、把编辑状态打开 //2、进行以下方法:-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath //=============================
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
//记录当前正在执行的操作,0代表删除,1代表增加
NSInteger action;
NSMutableArray *list;
}
//下面是工具栏的两个按钮,具体看图可知
@property (strong, nonatomic) IBOutlet UIBarButtonItem *addbtn;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *deletebtn;
@property (strong, nonatomic) IBOutlet UITableView *table;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
list = [[NSMutableArray alloc]initWithObjects:@"孙悟空",@"猪八戒",@"唐僧",@"狐狸精",@"蜘蛛精" ,@"小猫",@"小猪",@"小狗",nil];
action = 0;
self.table.delegate = self;
self.table.dataSource = self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSInteger rowOd = indexPath.row;
cell.textLabel.text = [list objectAtIndex:rowOd];
return cell;
}
//该方法的返回值作为删除指定表格行时确定按钮的文本,如图1
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
//=========================================
//要想实现增加删除操作,必须做到这三部,
//1、self.table.editing 即编辑状态要打开。为YES
//2、实现方法:-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//<即返回单元格的编辑状态>
//3、实现方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//<即进行删除增加操作>
//=========================================
//该方法是uitableviewdelegate协议中的方法,该方法的返回值决定单元格的编辑状态。
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//如果action的值为0,代表将要删除
return action == 0 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}
//
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[list removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}else
{
[list insertObject:[list objectAtIndex:indexPath.row] atIndex:indexPath.row+1];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
//uitableviewdatasource协议中定义的方法,该方法的返回值决定某行是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//第五个表格行不能编辑
if (indexPath.row == 5) {
return NO;
}
if ([[list objectAtIndex:indexPath.row] isEqualToString:@"孙悟空"]) {
return NO;
}
return YES;
}
//============================= //实现移动的操作只需两步 //1、把编辑状态打开 //2、进行以下方法:-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath //=============================
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSInteger sourceRow = sourceIndexPath.row;
NSInteger destRow = destinationIndexPath.row;
id targetObj = [list objectAtIndex:sourceRow];
[list removeObjectAtIndex:sourceRow];
[list insertObject:targetObj atIndex:destRow];
}
//这是是工具条上的两个按钮所绑定的方法
- (IBAction)toggleEdit:(id)sender {
if ([[sender title] isEqualToString:@"删除"]) {
action = 0;
}else
{
action = 1;
}
[self.table setEditing:!self.table.editing animated:YES];
if (self.table.editing) {
self.addbtn.title = @"完成";
self.deletebtn.title = @"完成";
}else
{
self.addbtn.title = @"增加";
self.deletebtn.title = @"删除";
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
上方图片1
相关文章推荐
- linux c文件操作函数
- 算法设计题3.18-栈和队列-第3章-《数据结构习题集》-严蔚敏吴伟民版
- 矩阵乘法并行优化
- warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式
- 204,UIApplication与代理方法
- Android 反编译apk + eclipse中调试smali
- HihoCoder(79 -80weeks)
- 【junit】java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch
- 如何选择合适的列建立索引
- 数据平台架构基于AWS的使用总结- Redshift优劣
- 字符串排序
- 华为OJ题目(十四):进制转换
- Educational Codeforces Round 5 E. Sum of Remainders 数论
- SQL Server学习记录之获取每月每季度每年第一天和最后一天
- iOS(五)基于XMPP的聊天:一
- 使用tcmalloc,它的central freelist很多,一直没有释放
- 使用maven编译dubbo,导入eclipse(其他maven开源项目编译类似)
- 重学C++ (五) 函数
- mac系统idea显示行号
- python二维数组