2012-02-12 16 views
16

私のコアデータをバックアップしようとしていますUITableViewに並べ替え機能があります。これらのデリゲートとコアデータのテクニックを実装した後here私は奇妙な動作を発見しました。編集ボタンをタップした後、並べ替えアイコンがうまく表示され、それをタップすると影が現れますが、別の行に移動しようとすると突然フォーカスが失われ、セルがその場所に戻ります。誰もがこの問題の原因を知っていますか?ここで並べ替え時に現在の位置からUITableViewCellをドラッグできない

は、ここで問題

http://www.youtube.com/watch?v=ugxuLNL7BnU&feature=youtu.be

を示すビデオは私のコードは、私は以下のコードとちょうど空の実装が、問題を試してみました-tableView:moveRowAtIndexPath:toIndexPath

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

ですまだ存在するので、私はこれが問題ではないとは思わない。

- (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デリゲート

- (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]; 
    } 
+0

'tableView commitEditingStyle:forRowAtIndexPath:'を実装し、 'UITableViewCellEditingStyleInsert'ケースを実装してデータソースを更新し、実際に行(または' reloadData')を移動しましたか? – lnafziger

+0

コアデータとfetchedResultControllerを使用しており、すべての保存部分は ' - (void)tableView:moveRowAtIndexPath:toIndexPath:' – sarunw

+0

で終了しました。もっとコードが追加されました: – lnafziger

答えて

18

は最後に、私は、問題に私が使用しているため、外部ライブラリを見つけIIViewDeckController、後に問題が離れて行くことを取り除きます。

+5

'IIViewDeckController'を保持したい場合は、 'viewDeck.panningMode = IIViewDeckNoPanning;' – rbrown

+0

私はちょうど同じ問題を抱えていました。これが私の問題を解決しました。あなたは私のヒーローです。 – JRod

+3

私は '' ECSlidingViewController''と同じ問題がありました。 '' [self.view removeGestureRecognizer:self.slidingViewController.panGesture];でスワイプジェスチャーを一時的に削除すると、この問題が解決されました。 – smek

3

実際これはViewDeckControllerによるものです。私はMonoTouchでポートを使用していますが、まだ問題に遭遇しています。それはUIPanGestureRecognizerに(より具体的に)期限が到来しています。ジェスチャ認識機能は、パン/ドラッグの動きをピックアップし、それ自体のためであると信じているので、UITableViewに送信されているタッチをキャンセルします。

残念ながら、UIPanGestureRecognizer.direction(horizo​​nal/vertical)プロパティはありませんが、特定の方向にのみパンジェスチャを制限する方法についてはUIPanGestureRecognizer - Only vertical or horizontalの投稿があります。上記の質問からサブクラス化されたバージョンを使用するようにUIPanGestureRecognizerを追加するViewDeckControllerコードを変更する必要があります。

私はこれをまだテストしていません(2amと今日は今日完了しました!)が、これを実装する時間があれば私の答えは更新されます。

迅速かつ汚い修正として、あなたは、これはしかし、それはまた、ビューでタッチをキャンセルしないで、その意図する結果を持って、あなたはあなたの項目を移動することができ、これに応じます

panner.cancelsTouchesInView = NO; 

を設定することができますビューのデッキを横にスワイプするときに、ユーザーがボタンを押すことがあります。

テーブルを編集モードに設定したときにこのプロパティをNOに設定し、完了後にこのプロパティをYESに戻すこともできます。

関連する問題