Modify custom button cell image inside NSTableView

Hi, I am using NSTableView, it contains 2 columns A and B

A contains data B contains buttons

I want when the user clicks on any button inside the table view, then its image should be changed.

How to add 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 the button, the selection method starts, but I don’t understand how to change the image of the button cell.

Any suggestions please !!!!!!!

Edit:

When I click 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]]];
    }
}
+3
source share
2 answers

Finally, I got the answer:

I found that the following method:

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

this method is called:

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

, -, .

+2

NSButtonCell, . . NSButtonCell ( ).

- :

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

Source: https://habr.com/ru/post/1795205/


All Articles