简体   繁体   English

通过单击按钮替换滑动删除操作

[英]Replace action of swipe to delete by clicking on a button

I have a tableview with a label and two buttons on each cell. 我有一个带有标签的tableview和每个单元格上的两个按钮。 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.
    }
} 

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链接。

- (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? 有谁能够帮助我?

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. 如果您的按钮位于单元格中,则可以将其tag属性设置为indexPath.row值,并为其指定deleteRow:(id)sender方法,通过-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents为了让按钮滑动,您可能需要实现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. 如果您打算通过滑动按钮删除,则可能需要禁用在桌面上滑动才能删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM