可能重复:
通过单击按钮替换滑动操作以删除
我有以下代码。
- (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.
}
}
当我在单元格上滑动时,将执行这段代码。但我想要的是当我在自定义表格视图单元格中按下按钮时执行此代码。就像您在下面的屏幕截图中看到的那样,我的 tableview 上有 2 个按钮。
当用户按下“X”按钮时,删除按钮应该像滑动单元格时一样展开。我的单元格附加了以下操作。
-(IBAction) deleteAppointment:(id) sender{
//show swipe delete button
}
并通过以下方式将其附加到我的 cellForRowAtIndex 中。
cell.deleteAppointment.tag = indexPath.row;
[cell.deleteAppointment addTarget:self action:@selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];