3

3 つのセクション (セクション 1、セクション 2、セクション 3) のテーブルがあります。ドラッグ&ドロップでセルの位置を変更する仕組みを実装したいと思います(同じセクション内またはセクションから別のセクションへ)。

例 1: セクション 1 には、2 つの行 (sec1_row1 と sec1_row2) があります。セクション 2 には、ドラッグ アンド ドロップ機能によって 1 つの行 (sec2_row1) があり、sec1_row2 を sec2_row1 で反転したいと考えています。sec2_row1 を選択し、sec2_row1 にドラッグ アンド ドロップして行を反転させたいと思います。

同じセクションの行でもこの機能を利用できるようにしてほしい。

この機能を実装する最良の方法は何ですか?

前もって感謝します。

4

4 に答える 4

2
- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
    toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
  {
      NSDictionary *section = [data objectAtIndex:sourceIndexPath.section];
      NSUInteger sectionCount = [[section valueForKey:@"content"] count];
      if (sourceIndexPath.section != proposedDestinationIndexPath.section)
      {
          NSUInteger rowInSourceSection =
          (sourceIndexPath.section > proposedDestinationIndexPath.section) ?
           0 : sectionCount - 1;
          return [NSIndexPath indexPathForRow:rowInSourceSection inSection:sourceIndexPath.section];
      } 
      else if (proposedDestinationIndexPath.row >= sectionCount) 
      {
          return [NSIndexPath indexPathForRow:sectionCount - 1 inSection:sourceIndexPath.section];
      }

     // Allow the proposed destination.
     return proposedDestinationIndexPath;
  }

詳細については、以下のリンクを確認してください。

行の並べ替えの管理

セクション外での並べ替えを防止したい場合は、このリンクを参照してください: UITableView 行の並べ替えをセクションに制限する方法

于 2013-10-25T09:24:49.903 に答える