2017-02-10 36 views
1

我在我的prog中實現了這三個方法來移動段中的行。是否有第二個選項在表格視圖中移動節中的行?

強制執行。這三種方法....

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 

    if (sourceIndexPath.section == 0) { 
     NSString *string = [arr1 objectAtIndex:sourceIndexPath.row]; 
     [arr1 removeObjectAtIndex:sourceIndexPath.row]; 
     [arr1 insertObject:string atIndex:destinationIndexPath.row]; 
    } else if (sourceIndexPath.section == 1) { 
     NSString *string = [arr2 objectAtIndex:sourceIndexPath.row]; 
     [arr2 removeObjectAtIndex:sourceIndexPath.row]; 
     [arr2 insertObject:string atIndex:destinationIndexPath.row]; 
    } else { 
     NSString *string = [arr3 objectAtIndex:sourceIndexPath.row]; 
     [arr3 removeObjectAtIndex:sourceIndexPath.row]; 
     [arr3 insertObject:string atIndex:destinationIndexPath.row]; 
    } 
} 
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{ 
    if(sourceIndexPath.section != proposedDestinationIndexPath.section) 
     { 
     return sourceIndexPath; 
     } 
     else 
     { 
     return proposedDestinationIndexPath; 
     } 
} 

是否有在目標C部分移動行任何第二個選項......

+2

而不是刪除並在數組中插入對象,則應該使用replaceObjectAtIndex。 – GeneCode

回答

0
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { 

    if (sourceIndexPath.section == 0) { 
     NSString *string = [arr1 objectAtIndex:sourceIndexPath.row]; 
     [arr1 exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; 
    } else if (sourceIndexPath.section == 1) { 
     [arr2 exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; 
    } else { 
     [arr3 exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; 
    } 
} 
相關問題