Re: Calling a setter from setObjectValue causes crash in NSCell subclass
Re: Calling a setter from setObjectValue causes crash in NSCell subclass
- Subject: Re: Calling a setter from setObjectValue causes crash in NSCell subclass
- From: Boris Remizov <email@hidden>
- Date: Mon, 27 Aug 2007 09:23:10 +0400
Crashing may be (and I think so) caused due the improper realization
of NSCopying protocol by the class returned from the
tableView:objectValueForTableColumn:row: delegate method.
On Aug 24, 2007, at 20:47 , Andrew Merenbach wrote:
Hi, all,
I have a cell that performs some custom drawing in a table view.
Oddly enough, it crashes the moment that I select any row in the
table view. This happens with two independent subclases of my
cell, so I don't think that the error's in my controller. The
debugger shows that the crash occurs in either -
setHighlightedAttributedString or -
setUnhighlightedAttributedString, both called by -
generateAttributedStrings, which is called from -setObjectValue:.
If I comment out the [self generateAttributedStrings] line in -
setObjectValue, everything works, but of course no data is
displayed in the cell. Does anyone have an idea as to what could
possibly cause a crash in accessor methods like mine? (As a couple
of side notes, calling -generateAttributedStrings from within my -
drawInteriorWithFrame:inView: method also yields a crash when the
cell is selector. Additionally, I've tried employing @synchronized
directives in my accessors, to no avail. It's not, as far I can
therefore tell, a threading issue.)
Here's the code for my cell subclass (I've also tried making this a
subclass of NSTextFieldCell, but that yielded the same crash--and
may have been a little silly, since I was not actually setting the
*cell's* attributedStringValue, just a couple of ivars):
- (id)initTextCell:(NSString *)aString {
if (nil != (self = [super initTextCell:aString])) {
[self setHighlightedAttributedString:nil];
[self setUnhighlightedAttributedString:nil];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (nil != (self = [super initWithCoder:aDecoder])) {
[self setHighlightedAttributedString:nil];
[self setUnhighlightedAttributedString:nil];
}
return self;
}
- (void)dealloc {
[self setHighlightedAttributedString:nil];
[self setUnhighlightedAttributedString:nil];
[super dealloc];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)
controlView
{
NSAttributedString *string = [self isHighlighted] ? [self
highlightedAttributedString] : [self unhighlightedAttributedString];
[controlView lockFocus];
NSRect insetRect = NSInsetRect(cellFrame, 5, 1);
[string drawInRect:insetRect];
[controlView unlockFocus];
}
- (void)setObjectValue:(id <NSCopying>)obj {
[super setObjectValue:obj];
[self generateAttributedStrings];
}
- (void)generateAttributedStrings {
[self setHighlightedAttributedString:[self
generatedAttributedString:YES]];
[self setUnhighlightedAttributedString:[self
generatedAttributedString:NO]];
}
- (NSAttributedString *)generatedAttributedString:(BOOL)highlighted {
NSAttributedString *string = [[[NSAttributedString alloc]
initWithString:@"hello world"] autorelease];
return string;
}
- (NSAttributedString *)highlightedAttributedString {
return [[AM_highlightedAttributedString retain] autorelease];
}
- (void)setHighlightedAttributedString:(NSAttributedString *)
anAttributedString {
if (anAttributedString != AM_highlightedAttributedString) {
[AM_highlightedAttributedString release];
AM_highlightedAttributedString = [anAttributedString copy];
}
}
- (NSAttributedString *)unhighlightedAttributedString {
return [[AM_unhighlightedAttributedString retain] autorelease];
}
- (void)setUnhighlightedAttributedString:(NSAttributedString *)
anAttributedString {
if (anAttributedString != AM_unhighlightedAttributedString) {
[AM_unhighlightedAttributedString release];
AM_unhighlightedAttributedString = [anAttributedString copy];
}
}
Cheers,
Andrew
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden