Swati
Swati

Reputation: 2918

Change image of custom button cell inside NSTableView

hi i am using an NSTableView it contains 2 columns A and B

A contains data B contains buttons

i want when user clicks on an any button inside the table view then its image must get changed.

How i add a custom button inside tableview cell:

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 setTarget:self];
    [buttonCell setAction:@selector(deleteSongContent:)];


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

When i click on the button the selector method gets fired but i dont understand how to change the image of button cell.

Any suggestions please!!!!!!!

Edit:

When i click on the button the method is called and how it works:

-(void)selectButtonsForDeletion:(NSTableView *)tableView 
{
  NSEvent *currentEvent = [[tableView window] currentEvent];
  int columnIndex = [tableView columnAtPoint:
                     [tableView convertPoint:
                      [currentEvent locationInWindow] fromView:nil]];
    NSTableColumn *column = [[tableView tableColumns]objectAtIndex:columnIndex];
    NSButtonCell *aCell = [[tableView tableColumnWithIdentifier:
                          [column identifier]]
                          dataCellForRow:[tableView selectedRow]];


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

Upvotes: 3

Views: 2312

Answers (2)

Swati
Swati

Reputation: 2918

Finally i got the answer:

I found that after below mentioned method:

- (void)tableView: setObjectValue: forTableColumn: row:

this method is called:

-(void)tableView: willDisplayCell: forTableColumn: row:

and i applied my logic that if check-image is already applied on the button then don't change it.

Upvotes: 2

Wim Haanstra
Wim Haanstra

Reputation: 5998

If all you want is to change the image on the NSButtonCell, then you can easily do this in the selector that is being called. The selector fired accepts a parameter. That parameter is the NSButtonCell object (if I am correct).

So why not do something like this:

- (void) deleteSongContent:(NSButtonCell*) button
{
    [button setImage[NSImage imageNamed:@"checked.png"]];
}

Upvotes: 1

Related Questions