Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac Catalyst: tableView allowmultipleselection not working

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
}
like image 301
ohomaidi Avatar asked Mar 25 '20 20:03

ohomaidi


Video Answer


2 Answers

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!

like image 126
Jens Schwarzer Avatar answered Sep 20 '22 16:09

Jens Schwarzer


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
like image 40
Smit Avatar answered Sep 23 '22 16:09

Smit