お伝えしたいポイントは次の3点です。
1) 特定の行がタップされたときに画像を移動したいですよね? Tableview
それでは、デリゲートのメソッドを使用しないのはなぜですかdidSelectRowAtIndexPath
?
2)UITableView
行を移動するために使用される方法があります。これはAppleのドキュメントからのものです:
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
指定された位置にある行を目的の位置に移動します。
タップされた行をセクション 0 行 0 に移動するコードは次のとおりです。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView beginUpdates];
[tableView moveRowAtIndexPath:path toIndexPath:indexPath];
[tableView moveRowAtIndexPath:indexPath toIndexPath:path];
[tableView endUpdates];
}
3) 3 番目のポイントがメインです。UITableView
デフォルトでは、並べ替えコントロールが提供されます。タップではなくドラッグして行を並べ替えたい場合は、次の手順に従ってこれを実現できます。
ステップ1:
テーブルビューを編集モードに設定します。通常、これは編集ボタンで行います。
[_yourTableView setEditing:YES animated:YES];
ステップ2:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
割り当て中
cell.showsReorderControl = YES;
ステップ 3:
UITableViewDataSource
のメソッドを実装する
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
// here you do all the reordering in your dataSource Array. Because dragging rows change the index of your row but the change should reflect in yopur array as well.
}
以上で、beginUpdates および endUpdates ブロックの下にコードを記述する必要はありません。この3つのステップを実行するだけです。
これを読んで、並べ替えのすべてを学びましょうTableView