2017-02-10 3 views
1

Ich implementierte diese drei Methoden in meinem Prog, um Zeilen mit in Abschnitt zu verschieben.Gibt es eine zweite Option zum Verschieben von Zeilen im Abschnitt in der Tabellenansicht?

ist zwingend erforderlich, diese drei Methoden zu implementieren ....

- (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; 
     } 
} 

Gibt es eine zweite Option Zeilen in Abschnitt in Objective-C zu bewegen ......

+2

Statt entfernen und Objekt in Array einfügen, sollten Sie replaceObjectAtIndex verwenden. – GeneCode

Antwort

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]; 
    } 
} 
Verwandte Themen