TableViewController的添加,删除,移动

#import "RootTableViewController.h"

@interface RootTableViewController ()

{

UITableViewCellEditingStyle _style;

}

@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation RootTableViewController

- (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self) {

// Custom initialization

self.array=[NSMutableArray array];

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

//在数组里存一些数据

for (int i=0; i<3; i++) {

NSMutableArray *tempArray=[NSMutableArray array];

for (int j=0; j<5; j++) {

[tempArray addObject:[NSString stringWithFormat:@"第%d组,第%d个人",i,j]];

}

[self.array addObject:tempArray];

}

// 设置代理

self.tableView.dataSource=self;

self.tableView.delegate=self;

//在导航栏两侧添加两个barbutton

UIBarButtonItem *bar=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction:)];

self.navigationItem.leftBarButtonItem=bar;

UIBarButtonItem *bar1=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction1:)];

self.navigationItem.rightBarButtonItem=bar1;

}

//barbutton编辑事件

-(void)barAction:(UIBarButtonItem *)sender

{

_style=UITableViewCellEditingStyleDelete;

BOOL flag=self.tableView.editing;

[self.tableView setEditing:!flag animated:YES];

}

//barbutton添加事件

-(void)barAction1:(UIBarButtonItem *)sender

{

_style=UITableViewCellEditingStyleInsert;

BOOL flag=self.tableView.editing;

[self.tableView setEditing:!flag animated:YES];

}

//设置哪些行能编辑(默认全部都能)

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

{

return  YES;

}

//确定编辑状态(删除|添加)

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

{

return _style;

}

//提交编辑

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

{

if(editingStyle==UITableViewCellEditingStyleDelete)

{

//删除

[self.array[indexPath.section]removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

}

else

{

//添加

NSString *s=@"测试数据";

[self.array[indexPath.section]insertObject:s atIndex:indexPath.row+1];

NSIndexPath *index=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationRight)];

}

}

//哪些行能移动

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

{

return YES;

}

//完成移动

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

//先赋值

id obj=self.array[sourceIndexPath.section][sourceIndexPath.row];

//再删除

[self.array[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

//最后添加

[self.array[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.section];

}

//检查越界

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

if(sourceIndexPath.section!=proposedDestinationIndexPath.section)

{

return sourceIndexPath;

}

else

{

return proposedDestinationIndexPath;

}

}

上一篇:liunx 开机流程与模块管理


下一篇:11 . Nginx核心原理讲解