In iOS 13 and later, you may present a context menu for a number of chosen rows in a UITableView by implementing the tableView(_:contextMenuConfigurationForRowAtIndexPath:level:) methodology and returning a UIContextMenuConfiguration object with the specified actions. Right here is an instance of how you might do that:
- (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath level:(CGPoint)level {
// create an array of chosen index paths
NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
// create a UIMenu with the specified actions
UIMenu *menu = [UIMenu menuWithTitle:@"Actions"
children:@[
[UIAction actionWithTitle:@"Action 1" image:nil identifier:nil handler:^(__kindof UIAction * _Nonnull action) {
// handle action 1
}],
[UIAction actionWithTitle:@"Action 2" image:nil identifier:nil handler:^(__kindof UIAction * _Nonnull action) {
// handle action 2
}]
]];
// create a UIContextMenuConfiguration with the menu and the chosen index paths
UIContextMenuConfiguration *config = [UIContextMenuConfiguration configurationWithIdentifier:nil previewProvider:nil actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
return menu;
}];
return config;
}
On this instance, the tableView(_:contextMenuConfigurationForRowAtIndexPath:level:) methodology creates an array of chosen index paths, then creates a UIMenu with the specified actions, and at last returns a UIContextMenuConfiguration object with the UIMenu and the chosen index paths. This could will let you present a context menu for a number of chosen rows in a UITableView.
In iOS 14 and later, UITableView has gained a brand new methodology, tableView(:contextMenuConfigurationForRowAt:level:), which lets you present a context menu for a number of chosen rows in a extra simple method. This methodology works equally to the collectionView(:contextMenuConfigurationForItemsAtIndexPaths:level:) methodology that was added in iOS 16.