get current selected button cell placed inside tableview using cocoa

Posted by Swati on Stack Overflow See other posts from Stack Overflow or by Swati
Published on 2011-03-01T07:18:52Z Indexed on 2011/03/02 7:24 UTC
Read the original article Hit count: 360

Filed under:
|

hi i have a NSTableView

i have two columns A and B B contains some data A contains custom button

the button is added to column A using this:

Below code is placed inside awakeFromNib method

 NSButtonCell *buttonCell = [[[NSButtonCell alloc] init] autorelease];
        [buttonCell setBordered:NO];
        [buttonCell setImagePosition:NSImageOnly];
        [buttonCell setButtonType:NSMomentaryChangeButton];
        [buttonCell setImage:[NSImage imageNamed:@"uncheck.png"]];
        [buttonCell setSelectable:TRUE];
        [buttonCell setTag:100];
        [buttonCell setTarget:self];
        [buttonCell setAction:@selector(selectButtonsForDeletion:)];

        [[myTable tableColumnWithIdentifier:@"EditIdentifier"] setDataCell:buttonCell];

Some code in display cell of nstableview:

-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
{
    if(tableView == myTable)
    {
        if([[tableColumn identifier] isEqualToString:@"DataIdentifier"])
        {
     }
        else if([[tableColumn identifier] isEqualToString:@"EditIdentifier"])
        {
            NSButtonCell  *zCell = (NSButtonCell *)cell;
            [zCell setTag:rowIndex];
            [zCell setTitle:@"abc"];
            [zCell setTarget:self];
            [zCell setAction:@selector(selectButtonsForDeletion:)];
        }
    }
}

now i want that when i click on the button the image of button cell gets changed as well as i want to do some coding.

When button gets clicked then by default the tableView's reference gets passed.

How can i get the button cell reference

i looked here for similar problem: Cocoa: how to nest a button inside a Table View cell?

but i am unable to add button inside column of NSTableView.

How i change the image:

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
{
    if(tableView == myTable)
    {
      if([[tableColumn identifier] isEqualToString:@"EditIdentifier"])
      {
          NSButtonCell *aCell = (NSButtonCell *)[[tableView tableColumnWithIdentifier:@"EditIdentifier"] dataCellForRow:row];


          NSInteger index = [[aCell title]intValue];
          if([self.selectedIndexesArray count]>0)
          {
              if(![self.selectedIndexesArray containsObject:[NSNumber numberWithInt:index]])
              {
                  [aCell setImage:[NSImage imageNamed:@"check.png"]];
                  [self.selectedIndexesArray addObject:[NSNumber numberWithInt:index]];
               }
              else 
              {
                  [aCell setImage:[NSImage imageNamed:@"uncheck.png"]];
                  [self.selectedIndexesArray removeObjectAtIndex:[selectedIndexesArray indexOfObject:[NSNumber numberWithInt:index]]];
              }
          }
          else {
              [aCell setImage:[NSImage imageNamed:@"check.png"]];
              [self.selectedIndexesArray addObject:[NSNumber numberWithInt:index]];
          }

      }
    }
}

I have debugged the code and found that proper tag and titles are passed but image applies on more than one button cell, this is too very irregular.

cant understand how its working!!!

Any suggestions what am i doing wrong??

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa