2016-07-22 15 views
0

Hallo allerseits Ich bin ein Neuling für iOS.Move UITableViewCell

Ich habe eine UITableView mit 3 Abschnitten in UITableViewController implementiert. Und ich habe die Abschnittszeilen für jeden Abschnitt angezeigt.

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 

    self.clearsSelectionOnViewWillAppear = NO; 

    self.navigationItem.rightBarButtonItem = self.editButtonItem; 

sectionTittle=[[NSMutableArray alloc]initWithObjects:@"Real Madrid",@"Barcelona",@"Liverpool", nil]; 
firstSection=[[NSMutableArray alloc]initWithObjects:@"Cr7",@"Garath Bale",@"Pepe", 
       nil]; 
secondSection=[[NSMutableArray alloc]initWithObjects:@"Lionel Messi",@"Neymar",@"Pique", 
       nil]; 
thirdSection=[[NSMutableArray alloc]initWithObjects:@"Gerrard",@"Saurez",@"Lallana", 
       nil]; 


     } 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return sectionTittle.count; 
    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 


if(section==0) 
{ 
    return [firstSection count]; 
} 
if(section==1) 
{ 
    return [secondSection count]; 
} 
if(section==2) 
{ 
    return [thirdSection count]; 
} 
return 0; 





    } 


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hi" forIndexPath:indexPath]; 


cell.textLabel.textColor=[UIColor brownColor]; 



if (indexPath.section == 0) 
{ 
    cell.textLabel.text=[firstSection objectAtIndex:indexPath.row]; 


} 


if (indexPath.section == 1) 
{ 
    cell.textLabel.text = [secondSection objectAtIndex:indexPath.row]; 
} 
if (indexPath.section == 2) 
{ 
    cell.textLabel.text = [thirdSection objectAtIndex:indexPath.row]; 
} 




// Configure the cell... 
    return cell; 
    } 

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    { 

return [sectionTittle objectAtIndex:section]; 
    } 

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
// Return NO if you do not want the item to be re-orderable. 
return YES; 
    } 

Jetzt sieht meine Ausgabe wie folgt:

ich die Zeilen zwischen den diesen drei Abschnitten verschieben möchten. Wie kann ich es tun..? Während ich es versuche, bewegt sich die Reihe zwischen dem Abschnitt, aber beim Speichern erzeugt es ein Problem.

Wie kann ich durchgehen ..?

Dank

Antwort

0

Sie müssen die UITableView-Nachbestellkapazität verwenden. Die Zellen können über Abschnitte hinweg neu geordnet werden.

[self.tableView setEditing:TRUE animated:TRUE]; 

Dies ermöglicht es dem Tableview im Bearbeitungsmodus die beinhaltet ermöglicht Löschmodus zusammen mit Nachbestellung: Um Nachbestellung-Modus zu aktivieren. Wenn Sie nicht möchten, dass der Löschmodus implementieren:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 
return UITableViewCellEditingStyleNone; 

}

Die Nachbestellung Unterstützung Zubehör Ansicht wird angezeigt, wenn Sie implementiert haben moveRowAtIndexPath wie, für Ihren Fall:

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

// Logic to change data source 
NSMutableArray *deleteFrom = [self getDataSourceArrayFromIndexPath:sourceIndexPath]; 
NSMutableArray *addTo = [self getDataSourceArrayFromIndexPath:destinationIndexPath]; 
[addTo insertObject:[deleteFrom objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row]; 
[deleteFrom removeObjectAtIndex:sourceIndexPath.row]; 

} 

wo getDataSourceArrayFromIndexPath ist:

-(NSMutableArray*) getDataSourceArrayFromIndexPath:(NSIndexPath*) index 
{ 
switch (index.section) { 
    case 0: 
     return self.firstSection; 
     break; 
    case 1: 
     return self.secondSection; 
     break; 
    case 2: 
     return self.thirdSection; 
     break; 
    default: 
     break; 
} 
return nil; 
} 
0

Verwenden Sie diesen Code

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 
    NSMutableArray *arrayTemp=[arrayMain objectAtIndex:sourceIndexPath.row]; 
    [arrayVisibleCountryList removeObjectAtIndex:sourceIndexPath.row]; 
    [arrayVisibleCountryList insertObject:arrayTemp atIndex:destinationIndexPath.row]; 
} 
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{ 
    //Uncomment these lines to enable moving a row just within it's current section 
    if ([sourceIndexPath section] != [proposedDestinationIndexPath section]) 
    { 
     proposedDestinationIndexPath = sourceIndexPath; 
    } 

    return proposedDestinationIndexPath; 
} 
+1

Was macht dieser Code? Wie funktioniert es? Und woher hast du es kopiert? –

0

können Sie unter Tableview-Methode verwenden. Es verschiebt Ihre Zelle von sourceIndexPath zu destinationIndexPath.

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