Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX Cocoa NSButton type checkbox " Select all " issue, and how to solve it?

Tags:

xcode

macos

cocoa

I have multiple checkboxes in a table view. And out of table, I have one checkbox with action select all that looks like this:

- (IBAction)selectAll:(NSButton *)sender {

    for (int i = 0; i < [mainTable numberOfRows]; i++) {

        NSTableCellView *cellView = [mainTable makeViewWithIdentifier:@"MainCell" owner:self];

        for(NSButton *button in cellView.subviews){

            //I have multiple buttons, i need one with toolTip
            if([[button toolTip] isEqualToString:@"select"]){

                [button setState:sender.state];

            }
        }           
    }

}

That code works great but VIEW IS NOT UPDATED. I tried [mainTable reloadData] but my guess that is not for that.

Simple question how to update view?

I tried:

1) Creating new NSButton and replacing old one but no luck there. View was not updated.

2) Trying to create NSButtons dynamically instead of using Designer and then applying code, no luck there. This was strange also. When I created dynamically elements in cellView only subviews was ones that i created with designer.

3) [button setNeedsDisplay:YES] also does not work;

I am not experienced Mac programmer.

UPDATE

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{

    NSDictionary *flag = [emails objectAtIndex:row];
    NSString *identifier = [tableColumn identifier];

    if([identifier isEqualToString:@"MainCell"]){
        NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];
        [cellView.textField setStringValue:flag[@"subject"]];
        cellView.textField.drawsBackground = YES;
        [cellView.textField setBackgroundColor:[NSColor whiteColor]];
        //[cellView setBackgroundStyle:NO];


        NSButton *checkBox;
        NSRect frame_checkbox = NSMakeRect(7, 35 , 317, 20);
        checkBox = [[NSButton alloc] initWithFrame:frame_checkbox];
        //NSLog(@"Message id: %@", flag[@"id"]);
        [checkBox setButtonType:NSSwitchButton];
        [checkBox setObjectValue:flag[@"id"]];
        [checkBox setState:NO];
        [checkBox setTitle:@""];
        [checkBox setToolTip:@"Select message with Id"];

I got this finally :

         if([flag[@"ischecked"] boolValue] == 1){ 
             [checkBox setState:YES]; 
         } 
         else{ 
           [checkBox setState:NO]; 
         } 
        [cellView addSubview:checkBox];

        //other subview shere


        return cellView;
    }
    return nil;
}

this part of code is for adding subviews.

NEW UPDATE

NSMutableArray *emails;
emails = [[NSMutableArray alloc] init];


    [emails addObject:@{@"id":@"1", @"from":@"some name",
     @"subject":@"some subject",
     @"email":@"some email", 
     @"date":@"10 min ago", 
     @"ischecked":@NO}];

Since this is NSMutableArray I do not have addObject. In order to do addObject this: NSMutableArray *emails has to be NSMutableDictionary *emails but I have no idea how to addObject to NSMutableDictionary;

If I do something like:

for(NSMutableDictionary *dict in emails){
   NSLog(@"%@", dict);
   [dict setObject:@"1" forKey:@"ischecked"];

}

I am getting error:

[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance
like image 375
Johnny Avatar asked Dec 10 '25 23:12

Johnny


1 Answers

Assuming emails is your data source:

NSDictionary *flag = [emails objectAtIndex:row];

flag is your dictionary at the indexPath.

You need to modify emails so that it holds the value for the checkbox then inside of viewForTableColumn you do:

[checkbox setState:flag[@"ischecked"] ? NSOffState : NSOnState];

and to check them all:

- (IBAction)selectAll:(NSButton *)sender {
    NSMutableArray *emailCopy = [[NSMutableArray alloc]init];

    for(NSDictionary *dict in emails){
       NSLog(@"dict = %@", dict);
       NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
       [newDict addEntriesFromDictionary:dict];
       [newDict setObject:@"1" forKey:@"ischecked"];
       [emailCopy addObject:newDict];
       NSLog(@"newDict = %@", newDict);
    }

    emails = emailCopy;
    [tableView reloadData];
}
like image 175
meda Avatar answered Dec 12 '25 13:12

meda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!