Programmatically add checkbox to cell

Hi,


I working with view based tables in MacOS X and was having a little diffculty with adding a checkbox to a cell programmatically. More specifically, centering it in the cell that it is being added. Basic question, but I'm stuck.


So, I have a view based table that I'm populating with calendar and reminder data. For rows that are calendar events, I'm populating one of the table cells with an image. For rows that contain a remdiner, I want to populate the same table cell with an NSButton. I've been able to do this, however, the checkbox is stuck with an origin of 0,0 within the cell. I'd like to move it over and up a bit to center it. I've tried changing the orgin after the NSButton is created and that did not work. I've also initialized the NSButton with a rect having the origin I want to have, and that doesn't work. What am I missing?


This is what I currently have in my "- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row" method:


            if (typeOfEvent == 1) { //this is a reminder
                NSRect checkBoxRect = NSMakeRect(25, 1, 20, 17);
                NSButton *toDoCheckBox = [[NSButton alloc] initWithFrame:checkBoxRect];
                [toDoCheckBox setButtonType:NSSwitchButton];
                [toDoCheckBox setTitle:@""];
                return toDoCheckBox;
            }
           
            if (typeOfEvent == 0) { //this is a calendar event
                NSColor *calendarColor;
                calendarColor = [[(EKEvent *)eventOrReminder calendar] color];
                NSImage *cellImage = [NSImage imageNamed:@"circle"];
               
                NSImage *coloredImage = [self coloredImage:calendarColor image:cellImage];
                [[cellView imageView] setImage:coloredImage];
                               
                return cellView;
            }


Any suggestions/help is appreciated.


Thanks,


Andy

It looks as though you're using the button as the cell view, as opposed to a subview within the cell view. Well, the table view governs the size and position of the cell view. It's always going to make it the size and position of the cell within the grid of rows and columns. That's just how it works. It doesn't matter what frame you assign.


For the calendar event / image view branch, you refer to a "cellView" variable that isn't declared or initialized in the code snippet you provided. It seems clear that it's a container view, though, as it seems to contain an image view.


That's the sort of thing you should do for the checkbox case. You should create a container view to serve as the cell view and add the checkbox button to it as a subview. You should consider using auto layout to positiion it within the cell view, but if you're not going to do that, you should probably set the autoresizingMask as appropriate.

Thank you. I appreciate the response.


Actually, that is what I had been doing. I had attached the button as a subview of the cellView but it never appeared in the table.


When I was playing around with it more today, I believe my mistake was that when I initialized the NSButton I just used the "init" method and I didn't set it's frame. When I do that, it correctly displays in the table cell when added as a subview of the cellView.


Thanks,


Andy

Programmatically add checkbox to cell
 
 
Q