75

私は、UIViewControllerTableViews デリゲートとデータソースプロトコルを実装する を持っています。「スワイプして削除」ジェスチャをセルに追加したいと思います。

どうすればいいですか。

メソッドの空の実装を指定しcommitEditingStyle、Editing プロパティを YES に設定しました。

まだスワイプ機能は来ていません。

UISwipeGesture各セルに個別に追加する必要がありますか?

または、何か不足していますか?

4

13 に答える 13

61

Danが上でコメントしたように、次のテーブル ビュー デリゲート メソッドを実装する必要があります。

  1. tableView:canEditRowAtIndexPath:
  2. tableView:commitEditingStyle:forRowAtIndexPath:

注: iOS 6 および iOS 7 でこれを試しました。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES - we will be able to delete all rows
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Perform the real delete action here. Note: you may need to check editing style
    //   if you do not perform delete only.
    NSLog(@"Deleted row.");
}
于 2013-10-07T09:20:30.340 に答える
55

editing:YESセルスワイプで削除ボタンを表示する必要がある場合は、設定する必要はありません。tableView:canEditRowAtIndexPath:編集/削除する必要がある行については、実装してそこから YES を返す必要があります。tableView の dataSource が UITableViewContoller のサブクラスである場合、これは必要ありません。このメソッドは、オーバーライドされていない場合、デフォルトで YES を返します。それ以外の場合はすべて、実装する必要があります。

編集:一緒に問題を発見しました -テーブルが編集モードでない場合にtableView:editingStyleForRowAtIndexPath:返されます。UITableViewCellEditingStyleNone

于 2012-01-24T07:07:47.067 に答える
25
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
于 2013-12-15T00:01:28.120 に答える
5

クラスに以下を追加してみてください。

// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return(YES);
}
于 2012-01-24T07:08:31.733 に答える
0

これは迅速なバージョンです

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
于 2015-05-06T09:22:20.037 に答える
0

iOS 8.0 以降では、アクションをカスタマイズできます。

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
于 2017-01-04T07:03:29.450 に答える
-1
NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; 


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [posts count];

    if (row < count) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}

- (void)tableView:(UITableView *)tableView 
                    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
                    forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [posts count];

    if (row < count) {

        [posts removeObjectAtIndex:row];
    }
}
于 2014-10-20T04:59:47.060 に答える