Domanda

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.

È stato utile?

Soluzione

You could try using the button' tag property setting it for each button you place as the number (location) in the tableview. Look here!!!

Detecting which UIButton was pressed in a UITableView

[EDIT1]

If people actually decided to read the linked post you would realize that the answer is actually there.

Try adding:

[button setTag:row];
[button addTarget:self action:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside];

inside the else of your viewForTableColumn routine:

In your checkBoxAction routine:

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

I also think that once you begin digging further into your code, you are going to want to start using the auto-dequeuing capability of the TableViewCell objects. I believe that you are going to find yourself in a memory alloc/dealloc problem.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top