【问题标题】:Segue from editActionsForRowAtIndexPath从 editActionsForRowAt IndexPath 转接
【发布时间】:2016-07-12 00:56:04
【问题描述】:

我正在制作一个小型银行类应用程序,用于教学目的。

所有交易都存储为核心数据实体,使用 Magical Record 辅助方法获取和排序。交易显示在UITableView 中,由fetchedObjects 数组提供。

我希望能够像这样使用editActionsForRowAtIndexPath

第一个动作转到另一个uiviewController,第二个和第三个动作转到UIPopoverPresentationController。第四个按钮删除事务。

编辑

这是创建按钮的代码:

- (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 *)myArray:(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
}

这是我的问题:

如何判断哪个按钮触发哪个 segue?我不知道如何建立联系。

这是来自我的prepareForSegue 的代表性代码块。 ???表示我的困惑之一在sourceRect 的参数中:

else if ([[segue identifier] isEqualToString:@"AmountPopSegue"])
{
    UIViewController *controller = segue.destinationViewController;
    controller.popoverPresentationController.delegate = self;
    controller.preferredContentSize = CGSizeMake(320, 186);
    UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;


    thisNavController = (UINavigationController *)segue.destinationViewController;
    AmountChangePopVC *acVC = (AmountChangePopVC *)thisNavController.topViewController;
    acVC.delegate = self;
    thisPPC.sourceRect = ???;
}

感谢所有见解。我用 Objective C 写作是因为我在 Swift 风靡一时之前就开始了这个项目。

【问题讨论】:

  • 你是如何实现一个单元格的多个动作的?第三方库还是使用原方法?
  • 我使用了 UiTableView 委托方法 canEditRowAtIndexPath 和 commitEditingStyle,然后是 editActionsForRowAtIndexPath。这是一个简单的实现,至少是其中的一部分。

标签: ios objective-c uitableview uitableviewrowaction


【解决方案1】:

这个方法可以帮到你。您会知道点击了哪个按钮。

--小心。此方法仅适用于 iOS 8 或更高版本。--

@property (nonatomic, strong) NSIndexPath *firstButtonIndexPath;
@property (nonatomic, strong) NSIndexPath *secondButtonIndexPath;
@property (nonatomic, strong) NSIndexPath *thirdButtonIndexPath;
@property (nonatomic, strong) NSIndexPath *fourthButtonIndexPath;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        if (self.firstButtonIndexPath) {
            NSLog(@"ViewButton :%ld", (long)self.firstButtonIndexPath.row);
        } else if (self.secondButtonIndexPath) {
            NSLog(@"Acct :%ld", (long)self.secondButtonIndexPath.row);
        } else if (self.thirdButtonIndexPath) {
            NSLog(@"Amt :%ld", (long)self.thirdButtonIndexPath.row);
        } else if (self.fourthButtonIndexPath) {
            NSLog(@"Delete :%ld", (long)self.fourthButtonIndexPath.row);
        }
    }
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.firstButtonIndexPath = nil;
    self.secondButtonIndexPath = nil;
    self.thirdButtonIndexPath = nil;
    self.fourthButtonIndexPath = nil;

    UITableViewRowAction *viewAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"View" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"View the row");
        self.firstButtonIndexPath = indexPath;
    }];
    viewAction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *acctAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Acct" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"Acct the row");
        self.secondButtonIndexPath = indexPath;
    }];
    acctAction.backgroundColor = [UIColor orangeColor];

    UITableViewRowAction *amtAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Amt" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"Amt the row");
        self.thirdButtonIndexPath = indexPath;
    }];
    amtAction.backgroundColor = [UIColor greenColor];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"Delete the row");
        self.fourthButtonIndexPath = indexPath;
    }];
    deleteAction.backgroundColor = [UIColor redColor];

    return @[deleteAction, amtAction, acctAction, viewAction];
}

【讨论】:

  • 我可能应该提到我已经实现了该方法。那么从结果数组中提取相关按钮的想法是什么?
  • 出于某种原因,我只看到了您的代码的下半部分。我会尽快尝试这个方法,让你知道它是否适合我。可能是一天左右,因为我还有其他事情要发生。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2022-10-15
相关资源
最近更新 更多