I have a tableview that allows multiple selection. I have set both allowsMultipleSelection and allowsMultipleSelectionDuringEditing to true in viewDidLoad and this is working perfectly on both iOS and iPadOS. I have decided to try out the Catalyst today and the app looks good except that I cannot select multiple rows in this view. Any ideas? Here is the code below. Many thanks in advance.
//allow multiple selection
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.allowsMultipleSelection = true
self.tableView.allowsMultipleSelectionDuringEditing = true
.....
}
//limit selection to 7 rows
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let selectedItems = tableView.indexPathsForSelectedRows {
if selectedItems.count > 6 {
return nil
}
}
return indexPath
}
@IBAction func doneButtonTapped(_ sender: UIBarButtonItem) {
...
let selectedIndexPaths = tableView.indexPathsForSelectedRows
if !selectedIndexPaths!.isEmpty {
for index in selectedIndexPaths! {
let selectedProcedure = fetchedResultsController?.object(at: index) as! Item
...
Rest of code to perform the required task
}
Great news! In macOS Big Sur UITableView tableView.allowsMultipleSelection = true
works just like in iOS! Happy days! You can also a select multiple cells programmatically!
Bellow is the solution wrriten in objective-c.
Thank you. @ph1lb4
#if TARGET_OS_MACCATALYST
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray<NSIndexPath *> * selectedRows = [tableView indexPathsForSelectedRows];
if ([selectedRows containsObject:indexPath]) {
[tableView deselectRowAtIndexPath:indexPath animated:false];
return nil;
}
return indexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray<NSIndexPath *> * selectedRows = [tableView indexPathsForSelectedRows];
if ([selectedRows containsObject:indexPath]) {
return nil;
}
return indexPath;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray<NSIndexPath *> * selectedRows = [tableView indexPathsForSelectedRows];
for(NSIndexPath *index in selectedRows){
[[tableView cellForRowAtIndexPath:index] setHighlighted:YES];
}
return YES;
}
#else
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath;
}
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With