So I subclassed NSCell added a NSButton and everything works good except for one thing. If row 2 in the table has a image so it has a button it shows it, but when I refresh the table with new data and row 2 (for the new data ) doesn't have an image so no button it shows the old button from the first set of data. Im pretty sure it's because of cell re-use but I can't put my finger on it. I've played with it alot but cannot find an answer, any thoughts or direction would be greatly appreciated.
Code:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSMutableDictionary *cellValues = [self object];
NSMutableDictionary *author = [cellValues objectForKey:@"from"];
NSMutableDictionary *comments = [cellValues objectForKey:@"comments"];
NSString *return_ = [NSString stringWithFormat:@"%@\n\nComments %@\n--%@",
[cellValues objectForKey:@"message"],
[comments objectForKey:@"count"],
[author objectForKey:@"name"]];
NSPoint textPoint;
button_ = [[NSButton alloc] init];
if([[cellValues valueForKey:@"type"] isEqualToString:@"photo"])
{
if([cellValues valueForKey:@"picDATA"]== nil)
{
[cellValues setObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:[cellValues objectForKey:@"picture"]]] forKey:@"picDATA"];
}
NSImage *image = [[NSImage alloc] initWithData:[cellValues valueForKey:@"picDATA"]];
[button_ setFrame:NSMakeRect(10, cellFrame.origin.y+5, 80, 80)];
[button_ setEnabled:YES];
[button_ setBezelStyle:NSShadowlessSquareBezelStyle];
[button_ setButtonType:NSMomentaryChangeButton];
[button_ setImage:image];
[button_ setImagePosition: NSImageOnly];
[button_ setBordered: NO];
[button_ setTag:row];
[button_ setAlternateImage:image];
[image release];
}
else if([[cellValues valueForKey:@"type"] isEqualToString:@"video"])
{
if([cellValues valueForKey:@"picDATA"]== nil)
{
[cellValues setObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:[cellValues objectForKey:@"picture"]]] forKey:@"picDATA"];
}
NSImage *image = [[NSImage alloc] initWithData:[cellValues valueForKey:@"picDATA"]];
[button_ setFrame:NSMakeRect(10, cellFrame.origin.y+5, 80, 80)];
[button_ setEnabled:YES];
[button_ setBezelStyle:NSRoundedBezelStyle];
[button_ setButtonType:NSMomentaryChangeButton];
[button_ setImage:image];
[button_ setImagePosition: NSImageOnly];
[button_ setBordered: NO];
[button_ setTag:row];
[button_ setAlternateImage:image];
[image release];
}else {
}
textPoint.x = cellFrame.origin.x+10;
textPoint.y = cellFrame.origin.y+5;
if([button_ image] != nil)
{
[button_ setTarget:delegate];
[button_ setAction:@selector(tappedImageButton:)];
[controlView addSubview:button_];
textPoint.x = textPoint.x + 110;
}
[button_ release];
NSFont *font = [NSFont systemFontOfSize:12];
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSLeftTextAlignment];
[pStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor blackColor], NSForegroundColorAttributeName,
pStyle, NSParagraphStyleAttributeName,
font, NSFontAttributeName,
nil];
[return_ drawInRect:NSRectFromCGRect(CGRectMake(textPoint.x, textPoint.y, 400, 900)) withAttributes:dict];
}