0

editActionsForRowAtIndexPathでセルを左スワイプしたときにいくつかのオプションを表示するように実装したいと考えていUITableviewます。だから私はこのコードを使用しました:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //Obviously, if this returns no, the edit option won't even populate
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //Nothing gets called here if you invoke `tableView:editActionsForRowAtIndexPath:` according to Apple docs so just leave this method blank
}

-(NSArray *)tableview:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        // Delete code here
                                    }];

    UITableViewRowAction *changeAmt = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Amt" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                       {
                                           // popover amount change code
                                       }];

    changeAmt.backgroundColor = [UIColor colorWithRed:0.022 green:0.541 blue:0.001 alpha:1.00];


    UITableViewRowAction *changeAcct = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Acct" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            // popover account change code
                                        }];

    changeAcct.backgroundColor = [UIColor colorWithRed:1.000 green:0.492 blue:0.000 alpha:1.00];

    UITableViewRowAction *viewTrans = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"View" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            // Push to view
                                        }];
    viewTrans.backgroundColor = [UIColor colorWithRed:0.002 green:0.342 blue:0.710 alpha:1.00];


    return @[delete,changeAmt,changeAcct,viewTrans]; //array with the buttons delete, changeAmt, changeAcct, viewTrans
}

昨日、このコードは次の結果を生成しました:

ここに画像の説明を入力

今日、次のように editActionsForRowAtIndexPath メソッドを変更して、viewTrans アクションを削除することにしました。

-(NSArray *)tableview:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        // Delete code here
                                    }];

    UITableViewRowAction *changeAmt = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Amt" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                       {
                                           // popover amount change code
                                       }];

    changeAmt.backgroundColor = [UIColor colorWithRed:0.022 green:0.541 blue:0.001 alpha:1.00];


    UITableViewRowAction *changeAcct = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Acct" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            // popover account change code
                                        }];

    changeAcct.backgroundColor = [UIColor colorWithRed:1.000 green:0.492 blue:0.000 alpha:1.00];

//    UITableViewRowAction *viewTrans = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"View" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
//                                        {
//                                            // Push to view
//                                        }];
//    viewTrans.backgroundColor = [UIColor colorWithRed:0.002 green:0.342 blue:0.710 alpha:1.00];


    return @[delete,changeAmt,changeAcct]; //array with the buttons delete, changeAmt, changeAcct, viewTrans
}

Deleteこの変更されたコードは、デフォルトのアクションを除くすべてのアクションを削除する効果がありました。

ここに画像の説明を入力

そのため、コードのコメントを外し、行内のviewTransアクションへの参照を置き換えましたreturn余分なアクションは再表示されません。

これは私が修正した唯一のコードです。誰が私が間違ったことを知っていますか?

4

1 に答える 1

0

私はあなたの質問に取り組んでいましたが、最初はeditActionsForRowAtIndexPathメソッドを呼び出すことができませんでしたが、後で少し問題が見つかりました。おそらくあなたにとっても同じです。メソッドの宣言

-(NSArray *)tableview:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 

は正しくありません。正しいのは

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

だから私の方法はこのままです

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        // Delete code here
                                    }];

    UITableViewRowAction *changeAmt = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Amt" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                       {
                                           // popover amount change code
                                       }];

    changeAmt.backgroundColor = [UIColor colorWithRed:0.022 green:0.541 blue:0.001 alpha:1.00];


    UITableViewRowAction *changeAcct = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Acct" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            // popover account change code
                                        }];

    changeAcct.backgroundColor = [UIColor colorWithRed:1.000 green:0.492 blue:0.000 alpha:1.00];

    //UITableViewRowAction *viewTrans = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"View" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    //                                   {
                                           // Push to view
    //                                   }];
    //viewTrans.backgroundColor = [UIColor colorWithRed:0.002 green:0.342 blue:0.710 alpha:1.00];


    return @[delete,changeAmt,changeAcct]; //array with the buttons delete, changeAmt, changeAcct, viewTrans

}

そして、これはそれがどのように見えるかです

ここに画像の説明を入力

これがお役に立てば幸いです

于 2016-07-14T19:33:02.873 に答える