0

カスタムセルを含むテーブルビューがあります。セルは私のデータでいっぱいです。ここで、ユーザーが行を再配置できるようにします。メソッドを実装しましたが、ドラッグしてセルを並べ替えると、実行しようとしているように見えますが、どこにも移動できません。再配置を行うかのように10ピクセルのように移動しますが、元の位置に戻ります。カスタムセルで行を並べ替える方法は?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (editingStyle == UITableViewCellEditingStyleDelete)  
    {
       [self.dataSource removeObjectAtIndex:indexPath.row];
       [tableView reloadData];
    }
}

-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath 
{
    if (self.mytableView.editing) 
    {
            return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}

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

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

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{
    id stringToMove = [self.dataSource objectAtIndex:sourceIndexPath.row];

    [self.dataSource removeObjectAtIndex:sourceIndexPath.row];

    [self.dataSource insertObject:stringToMove atIndex:destinationIndexPath.row];
}

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{
    if (proposedDestinationIndexPath.section != sourceIndexPath.section) 
    {
            return sourceIndexPath;
    }
    return proposedDestinationIndexPath;
}
4

1 に答える 1

1

私はこれが古いことを知っていますが、それでも答えます。ここでの問題はあなたのtableView: targetIndexPathForMoveFromRowAtIndexPath: toProposedIndexPath:方法(あなたの最後の方法)にあります

あなたの論理は、動きが起こらないようにしています。あなたのifステートメント:

if (proposedDestinationIndexPath.section != sourceIndexPath.section)

目的の場所(ユーザーがセルを移動したい場所)が現在の場所ではない場合は、現在の場所を返します(セルを移動しないでください)。それ以外の場合、目的の場所(行きたい新しい場所)が現在の場所である場合は、目的の場所(実際には現在の場所)を返します

それが理にかなっていることを願っています。基本的には、何があっても、すべてのセルが常に現在の場所にあることを確認してください。これを修正するには、このメソッドを削除するか(違法な移動がない限り、これは必要ありません)、2つのreturnステートメントを切り替えます。

-(NSIndexPath *)tableView:(UITableView *)tableView 
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
      toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {

    if (proposedDestinationIndexPath.section != sourceIndexPath.section) {
        return proposedDestinationIndexPath;
    }
    return sourceIndexPath;
}

実際、再配置を可能にするために必要な唯一の方法は次のとおりtableView: moveRowAtIndexPath: toIndexPath:です。繰り返しになりますが、他のメソッドから特定の動作が必要な場合を除いて、コードを保存してほとんどを削除することができます(特に、この状況では主にデフォルトを実装しているだけなので)。

于 2013-06-27T22:22:01.720 に答える