//某一行是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPatt
{
return NO;
}
//确认移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if(sourceIndexPath.section == destinationIndexPath.section){
//获取要移动元素所在的组(子数组)
NSMutableArray *subArray = [_dataArray objectAtIndex:sourceIndexPath.section];
//获得要移动的对象
DataItem *item = [subArray objectAtIndex:sourceIndexPath.row];
//删除
[subArray removeObject:item];
//将他插入到新位置
[subArray insertObject:item atIndex:destinationIndexPath.row];
}else{
//获得要移动的对象
NSMutableArray *subArray = [_dataArray objectAtIndex:sourceIndexPath.section];
DataItem *item = [subArray objectAtIndex:sourceIndexPath.row];
[subArray removeObject:item];
//获得目标数组
NSMutableArray *destArray = [_dataArray objectAtIndex:destinationIndexPath.section];
//插入新元素
[destArray insertObject:item atIndex:destinationIndexPath.row];
}
}
//编辑方式 删除或者插入
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row%2!=0){
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
//是否可以编辑(默认是可以编辑的)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;//indexPath.row%2==0;
}
//确认编辑的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleInsert){
NSMutableArray *subArray = [_dataArray objectAtIndex:indexPath.section];
DataItem *oldItem = [subArray lastObject];
NSRange range =[oldItem.title rangeOfString:@":"];
NSString *replaceStr = [oldItem.title substringFromIndex:range.location];
DataItem *item = [[DataItem alloc]init];
item.title = [oldItem.title stringByReplacingOccurrencesOfString:replaceStr withString:[NSString stringWithFormat:@"%d行",[subArray count]]];
item.state = 0;
[subArray insertObject:item atIndex:indexPath.row];
[_myTalbelView reloadData];
}else {
NSMutableArray *subArray = [_dataArray objectAtIndex:indexPath.section];
[subArray removeObjectAtIndex:indexPath.row];
//刷新界面(必须加这句,不然点删除删不了)
[_myTalbelView reloadData];
}
}
//索引对应关系
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return (index+2)%26;
}
//删除样式
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row%4==0){
return @"删除";
}
return @"Remove";
}