tableView中cell的删除、插入、移动、复制粘贴有关问题详解代码分析

tableView中cell的删除、插入、移动、复制粘贴问题详解代码分析


//可编辑操作改为yes  当移动、插入的时候必须设为yes,删除的可以设置可以不设置
    self.tableView.editing = YES;
//////////////////////////////////////////////

#pragma mark - 可删除
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return YES;
}
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return UITableViewCellEditingStyleDelete;
}
-(
void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
CarGroupModel *carGroup = self.carGroupArray[indexPath.section];
   
if (editingStyle == UITableViewCellEditingStyleDelete) {
        [carGroup.
cars removeObjectAtIndex:indexPath.row];
    }
    [
self.tableView reloadData];
}

#pragma mark - 可插入数据
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return YES;
}
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return UITableViewCellEditingStyleInsert;
}
-(
void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
CarGroupModel *carGroup = self.carGroupArray[indexPath.section];
   
CarModel *car = [[CarModel alloc]init];
    car.
name = @"1111";
   
if (editingStyle == UITableViewCellEditingStyleInsert) {
        [carGroup.
cars insertObject:car atIndex:indexPath.row];
    }
    [
self.tableView reloadData];
}
#pragma mark - 可移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return YES;
}
-(
void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
   
CarGroupModel *carGroupS = self.carGroupArray[sourceIndexPath.section];
    [carGroupS.
cars exchangeObjectAtIndex:destinationIndexPath.row withObjectAtIndex:sourceIndexPath.row];
}
#pragma mark - 可复制可粘贴
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return YES;
}
-(
BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
   
return YES;
}
-(
void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
   
CarGroupModel *carGroup = self.carGroupArray[indexPath.section];
   
CarModel *car = carGroup.cars[indexPath.row];
   
if (action == @selector(copy:)) {
        [
UIPasteboard generalPasteboard].string = car.name;
    }
   
if (action == @selector(paste:)) {
       
NSString *name = [UIPasteboard generalPasteboard].string;
        car.
name = name;
      
// [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [
self.tableView reloadData];
    }
}