13

UITableViewCell をカスタマイズしました。「スワイプして削除」を実装したいと考えています。しかし、デフォルトの削除ボタンは必要ありません。その代わり、何か違うことをしたい。これを実装する最も簡単な方法は何でしょうか? ユーザーがスワイプしてセルを削除したときに呼び出されるメソッドはありますか? デフォルトの削除ボタンが表示されないようにすることはできますか?

現在、UITableViewCellのデフォルト実装でスワイプして削除する際に発生するデフォルトの削除ボタンと縮小アニメーションを回避するために、独自のロジックを実装する必要があると思います。

たぶん、UIGestureRecognizer を使用する必要がありますか?

4

2 に答える 2

16

まったく異なることをしたい場合は、UISwipeGestureRecognizer を各テーブルビュー セルに追加します。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.


    UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
    [sgr setDirection:UISwipeGestureRecognizerDirectionRight];
    [cell addGestureRecognizer:sgr];
    [sgr release];

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
    // ...
    return cell;
}

- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
        NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
        //..
    }
}
于 2011-05-29T12:55:53.917 に答える
16

削除ボタンを回避するために使用できる 2 つの方法を次に示します。

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
于 2012-08-19T19:34:05.317 に答える