0

行のボタンがタップされたときに行を別のセクションにコピーしたい.

-(void)moveRowToAnotherSection:(id)sender{

   UIButton *button = (UIButton *)sender;
   UITableViewCell *cell = (UITableViewCell *)button.superview;
   NSMutableArray *tempArr = [[NSMutableArray alloc] init];
   [[self tableView] beginUpdates];

    [tempArr addObject:[NSIndexPath indexPathForRow:self.favouritesArray.count inSection:0]];
    [self.favouritesArray insertObject:cell.textLabel.text atIndex:self.favouritesArray.count];
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArr withRowAnimation:UITableViewRowAnimationFade];

   [[self tableView] endUpdates];

}
4

1 に答える 1

0

お伝えしたいポイントは次の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

于 2013-08-07T11:47:16.783 に答える