您的位置:首页 > 产品设计 > UI/UE

UITableView 的编辑

2015-08-13 19:28 381 查看
准备工作

@property(nonatomic, retain)UITableView *tableView
@property(nonatomic, retain)NSMUtableArray *arr

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

self.navigationController.navigationBar.translucent = NO;
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 365, 665 - 64) style:UITableViewStylePlain];

[self.view addSubview:self.tableView];
[_tableView release];

self.tableView.delegate = self;
self.tableView.dataSource = self;

self.navigationItem.rightBarButtonItem = 0

}

// 方法
// 重写系统的编辑按钮, 点击触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:Yes];

}

// 设置哪些行科技进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 奇数行可以编辑, 偶数行不能编辑
if (indexPath.row % 2 == 0) {
return NO;
} else {
return YES;
}
}

// 样式: 两种样式, 一个是插入, 一个是删除

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (index.row % 2 == 1){
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellStyle
}

// 删除数据

- (void)tableView:(UITableVIew *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.arr removeObjectAtIndex:indexPath.row};
[self.tableView reloadDate]; 或
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

if (editingStyle == UITableViewCellEDitingStyleInsert) {
[self.arr insertObject:@"字符串" atIndex:indexPath.row];
[self.tableView reloadData]; 或
[self.tableView insertRowAtIndexPath:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}

// 修改删除按键

- (NSString *)tableView:(UITableView *)tableView titleForDeleteContirmationButtonForRowAtIndexPath:(NSIndexPath *)indexpath
{
return  @"来点我啊";
}

// 这个方法是iOS8.0 之后出现的方法, 可以在编辑的状态时候有多个按钮

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowactionStyleDelfault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// 按键的点击触发的事件都写在block中
NSLog(@"出发了删除按钮");
[self.arr removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPath:@[indexPath] withRowAnimation:UITableViewRowAnimationLet];
}];

UITableViewRowAction *addAction = [UITableViewRowAction rowActWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

NSLog(@"你是谁"):
[self.arr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
[self.tableView reloadData];
}];

deleteAction.backgroundColor = [UIColor grayColor];

return @[deleteAction addAction];
}

// 移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1. 现获取起始位置的数据
NSString *str = [self.arr{sourceIndexPath.row] retain];
// 2. 把起始位置的对象从数据中离开
[self.arr removeObjectAtIndex:sourceIndexPath.row];
// 3. 把数据插入到数组的目的位置上去
[self.arr insertObject:str atIndex: destinationIndexPath.row];
[self.tableView relocate];
[str release];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: