- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle == UITableViewCellEditingStyleDelete){
//[theSimpsons removeObjectAtIndex:indexPath.row];
NSString *time = [[arrayList objectAtIndex:indexPath.row] objectForKey:@"TIME"];
NSString *date= [[arrayList objectAtIndex:indexPath.row] objectForKey:@"DATE"];
DBManager *dbm = [[DBManager alloc] init];
[dbm initiallzeDatabase];
[dbm deleteItemAtIndexPath:time :date];
//arrayList = [dbm getParkResults];
//[parkTableView reloadData];
[arrayList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
这是我应用中的部分代码,编辑部分很正常,只是删除键只有在单元中从左至右滑动时才显示。现在我有一个从左至右打开的菜单,我想让用户从右至左滑动时,删除键也会显示。
ChatGPT尝试为您解答,仅供参考
您可以通过实现表视图的 tableView:leadingSwipeActionsConfigurationForRowAtIndexPath: 方法来实现从右向左滑动时显示删除按钮。
这是您可以使用的一个例子:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create a delete action
UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
// Perform delete action here
// Remove the item from the data source
[arrayList removeObjectAtIndex:indexPath.row];
// Delete the row from the table view
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Call completion handler to dismiss the action button
completionHandler(YES);
}];
// Create and return a swipe actions configuration with the delete action
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
return config;
}
您也可以通过实现表视图的 tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: 方法来实现从右向左滑动时显示删除按钮。
这是您可以使用的一个例子:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create a delete action
UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
// Perform delete action here
// Remove the item from the data source
[arrayList removeObjectAtIndex:indexPath.row];
// Delete the row from the table view
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Call completion handler to dismiss the action button
completionHandler(YES);
}];
// Create and return a swipe actions configuration with the delete action
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
return config;
}
请注意,您可能还需要在表视图的代理中设置 editingStyleForRowAtIndexPath: 为 UITableViewCellEditingStyleDelete,以便当用户从左向右滑动时也显示删除按钮。