Sto cercando di rendere i miei dati Core supportati UITableView ha capacità di riordino, Dopo aver implementato tutti i delegati e qualche tecnica per i dati di base menzionati here ho trovato un comportamento strano. Dopo aver toccato il pulsante di modifica, l'icona di riordino viene visualizzata correttamente e posso toccarla, l'ombra viene visualizzata, ma quando provo a spostarla su un'altra riga, improvvisamente perdo la messa a fuoco e la cella torna al suo posto. Qualcuno conosce la causa di questo problema?Impossibile trascinare UITableViewCell dalla sua posizione corrente durante il riordino
Qui è il video che mostra il problema
http://www.youtube.com/watch?v=ugxuLNL7BnU&feature=youtu.be
Ecco il mio codice
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
in -tableView:moveRowAtIndexPath:toIndexPath
Ho provato il codice qui sotto e solo un'implementazione vuota, ma il problema esistono ancora, quindi non penso che questo non sia un problema.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
_changeIsUserDriven = YES;
NSUInteger fromIndex = sourceIndexPath.row;
NSUInteger toIndex = destinationIndexPath.row;
NSMutableArray *newsArray = [[self.fetchedResultsController fetchedObjects] mutableCopy];
News *news = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];
[newsArray removeObject:news];
[newsArray insertObject:news atIndex:toIndex];
int i = 1;
for (News *n in newsArray) {
n.displayOrder = [NSNumber numberWithInt:i++];
}
[self saveContext];
_changeIsUserDriven = NO;
}
FetchedResultController delegato
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
if (_changeIsUserDriven) {
return;
}
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (_changeIsUserDriven) {
return;
}
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (_changeIsUserDriven) {
return;
}
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (_changeIsUserDriven) {
return;
}
[self.tableView endUpdates];
}
Hai implementato 'tableView commitEditingStyle: forRowAtIndexPath:' e implementa il caso 'UITableViewCellEditingStyleInsert' per aggiornare l'origine dati e spostare effettivamente la riga (o' reloadData')? – lnafziger
Io uso i dati di base e fetchedResultController per questo e tutta la parte di salvataggio viene eseguita in '- (void) tableView: moveRowAtIndexPath: toIndexPath:' – sarunw
Ok, ora che c'è più codice: – lnafziger