通过单击按钮替换滑动删除操作 [英] Replace action of swipe to delete by clicking on a button

查看:94
本文介绍了通过单击按钮替换滑动删除操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标签的tableview,每个单元格上有两个按钮。目前我有这个代码可以刷卡删除。

I have a tableview with a label and two buttons on each cell. At the moment I have this code that does a swipe to delete.

- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [appointments removeObjectAtIndex: indexPath.row];  // manipulate your data structure.
        [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath]
                         withRowAnimation: UITableViewRowAnimationFade];
       NSLog(@"row deleted");  // Do whatever other UI updating you need to do.
    }
} 

当我滑动时,此代码会执行。但是我希望它在按下按钮时执行。该按钮与以下IBAction链接。

This code gets executing when I swipe. But I want it to execute when a button is pressed. The button is linked with the following IBAction.

- (IBAction)deleteRow:(id)sender {
  //Delete row
}

要记住一件事。我希望删除按钮像滑动删除时一样滑动。

One thing to keep in mind. I want that the delete button slides like when you swiped to delete.

任何人都可以帮助我吗?

Can anybody help me?

推荐答案

如果你的按钮在在单元格中,您可以将其tag属性设置为indexPath.row值,并通过为其指定deleteRow:(id)sender方法 - (void)addTarget:(id)target action:(SEL) action forControlEvents:(UIControlEvents)controlEvents 为了让按钮滑动,您可能需要实现panGestureRecognizer。我可能错了,但这个想法应该是检测触摸位置和滑动按钮(滑动时设置框架)。如果您打算通过滑动按钮删除,则可能需要禁用在桌面上滑动才能删除。

If your button is in the cell, you could set its tag property to the indexPath.row value and assign it the deleteRow:(id)sender method for target via -(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents In order to have the button slide, you may need to implement the panGestureRecognizer. I may be wrong about this but the idea should be pretty much to detect the touch locations & slide the button (setting its frame as you slide). And you may need to disable swipe to delete on the table if your intention is to delete by sliding the button.

这篇关于通过单击按钮替换滑动删除操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆