Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with a checkBox in a viewbased NSTableView

I have an NSDictionary that holds all the data:

  • One title (not important for this question)
  • One link (not important for this question)
  • One array of NSDictionary containing again 1 title and 1 link

I'm displaying this data in a view based table view like this:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
    if (tv == _downloadTable) 
    //I use this "if" because I have another tableView that has nothing to do
    //with this one
    {
        return [[myDictionary objectForKey:@"myArray"] count];
    }
}

I want 2 columns in this tableView, one to display the title and one with a checkbox, that would do something letting me know which row is checked.

- (NSView *)tableView:(NSTableView *)tv viewForTableColumn :(NSTableColumn *)tableColumn row :(NSInteger)row 
{
    if (tv == _downloadTable) 
    {
        if (tableColumn == _downloadTableTitleColumn) 
        {
            if ([[[myDictionary         objectForKey:@"myArray"]objectAtIndex:row]objectForKey:@"title"]) 
            {
            NSString *title = [[[myDictionary objectForKey:@"myArray"]objectAtIndex:row]objectForKey:@"title"];
            NSTableCellView *result = [tv makeViewWithIdentifier:tableColumn.identifier owner:self];
            result.textField.stringValue = title;
            return result;
            }
        }
       if (tableColumn == _downloadTableCheckColumn) 
       {
           NSLog(@"CheckBox"); //I wanted to see exactly when that was called
                               //But it didn't help me :(
           NSButton *button = [[NSButton alloc]init];
           [button setButtonType:NSSwitchButton];
           [button setTitle:@""];
           return button;
       }
   }
}

Right now when I run it and click on the checkbox it does nothing (of course because I don't know how to make it do something. Where should I put the code that should do something?

The main goal is an editable list of downloads, right now the list is displayed, with the checkbox right next to the title at each lines. I would like to know which checkBox are checked and which are not.

I tried this:

[button setAction:@selector(checkBoxAction:)];

- (void)checkBoxAction: (id)sender
{
   NSLog(@"I am button : %@ and my state is %ld", sender, (long)[sender state]);
}

But I can't figure out how to get the row of that button, to know which title is associated with this checkBox.

I also tried the setObjectValue method of the tableView without success.

The way I would like it to work is:

I have a "start downloading" button that check if each checkbox is checked or not and launch the next action (downloading) only with the checked row.

I would like to avoid bindings because I plan to make it work on iOS too and I don't want to have different code for iOS.

like image 390
ItsASecret Avatar asked Jun 27 '12 22:06

ItsASecret


1 Answers

You can use the NSTableView method -rowForView: to get the row a particular view is in.

In your case you'd have something like this:

- (void)checkBoxAction:(id)sender
{
    NSInteger row = [_downloadTable rowForView:sender];
    NSLog(@"The button at row %ld was clicked.", row);
}

Here are the docs for NSTableView: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/Reference/Reference.html

like image 107
sam Avatar answered Oct 15 '22 00:10

sam