#pragma mark TableView编辑
#warning 第一步:让tableView处于编辑状态(Warning警告的意思)
- (void)editButton:(id)sender
{
// edit:添加事件
[_table setEditing:YES animated:YES];
}
#warning 第二步:通知tableview哪些cell可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row == 0) {
// return YES;
// }
// return NO;
return YES;
}
#warning 第三步:cell的编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
}
#warning 第四步:提交编辑结果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 如果做的是删除操作,就把相对应的数组的那一行给删除了
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_tableArray removeObjectAtIndex:indexPath.row];
//
// // 数据源(数组)发生改变,则必须让tableView的所有协议重新再走一遍,告诉tableView关于行数、内容的改变是什么
// // 刷新tableView (最经常用的刷新方式)
// [tableView reloadData];
// 删除单个或多个行 数组中存得就是索要删除的IndexPath
NSArray *array = @[indexPath];
// // 如果没有Index的话,就自己创建(自己创建IndexPath的方法)
// NSIndexPath *myIndex = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
StudentModel *s4 = [StudentModel studentModerWithName:@"老王" gender:@"男" teleNumber:@"15840959355"];
[_tableArray addObject:s4];
// [tableView reloadData];
NSIndexPath *myIndex = [NSIndexPath indexPathForRow:[_tableArray count] - 1 inSection:0];
[tableView insertRowsAtIndexPaths:@[myIndex] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark -
#pragma mark 移动
// 是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// [tableView reloadData];
return YES;
}
// 移动sourceIndexPath:将要移动的 destinationIndexPath:目标
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 先改变数据源的顺序(从数组中拿出来的时候release,所以要先retain)
StudentModel *model = [[_tableArray objectAtIndex:sourceIndexPath.row] retain];
[_tableArray removeObjectAtIndex:sourceIndexPath.row];
[_tableArray insertObject:model atIndex:destinationIndexPath.row];
[tableView reloadData];
[model release];
}