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];
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章