【问题标题】:Disappearing editActionsForRowAtIndexPath actions消失的 editActionsForRowAt IndexPath 动作
【发布时间】:2016-07-12 19:16:41
【问题描述】:

当我在UITableview 中向左滑动单元格时,我想实现editActionsForRowAtIndexPath 以显示多个选项。所以我使用了这段代码:

- (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 操作之外的所有操作:

所以我取消了代码的注释,并替换了return 行中对viewTrans 操作的引用。 额外的动作不会再次出现。

这是我修改的唯一代码。有人知道我做错了什么吗?

【问题讨论】:

    标签: ios uitableview uitableviewrowaction


    【解决方案1】:

    我一直在处理你的问题,最初,我无法调用 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
    
    }
    

    这就是它的样子

    希望对你有帮助

    【讨论】:

    • 谢谢,雷尼尔。我对此很陌生,所以我不完全理解,但我会研究这种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多