UITableViewCell copyWithZone unrecognized selector sent to instance

1.4k Views Asked by At

I am trying to update the values in my custom tableviewcell (DHtableViewCell) using KVO. I keep getting this error. I am aware that there are others that have this same exception, but their solutions do not help.

-[DHTableViewCell copyWithZone:]: unrecognized selector sent to instance 0x1093958b0
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[DHTableViewCell copyWithZone:]: unrecognized selector sent to instance 0x1093958b0'
*** First throw call stack:
(
0   CoreFoundation  0x0000000101a40495 __exceptionPreprocess + 165
1   libobjc.A.dylib 0x000000010179f99e objc_exception_throw + 43
2   CoreFoundation  0x0000000101ad165d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation  0x0000000101a31d8d ___forwarding___ + 973
4   CoreFoundation  0x0000000101a31938 _CF_forwarding_prep_0 + 120
5   UIKit           0x00000001004cbee0 -[UILabel _setText:] + 126
6   GoalTasker328   0x0000000100001ae4 -[DHTableViewCell observeValueForKeyPath:ofObject:change:context:] + 276
7   Foundation      0x000000010139eea2 NSKeyValueNotifyObserver + 375
8   Foundation      0x00000001013a06f0 NSKeyValueDidChange + 467
9   Foundation      0x000000010136379c -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118

The class that implements the tableView:cellForIndexPath sets a property called description

//.h
@interface DHTableViewCell: UITableViewCell

@property (strong, nonatomic) NSString *description;

@end


//*.m
@interface DHTableViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *detailsOfTask;

@end

- (void)awakeFromNib
{
    // Initialization code
    self.description = @"";

    [self addObserver:self
           forKeyPath:NSStringFromSelector(@selector(description))
              options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(description))]) {
        [[self detailsOfTask] setText:(NSString *)object]; //**crashes here!!!!
    } 
}

So the way the code works is, in the tableview:cellForIndexpath method I set the cell.description property. Then the observer sees that the value has changed then it goes and updates the text that the UILabel that corresponds to.

I am stumped. Why is it trying to call copyWithZone? How can I work around this?

1

There are 1 best solutions below

0
On

The problem is that the object parameter in the call to observeValueForKeyPath:ofObject:change:context: is your cell instance. You then simply cast the cell instance to NSString. The label is trying to make a copy of the string. Since this call to copy is on the cell instance instead of some NSString, you get the crash.

While I don't see the point to listening for changes to the cell's description, one solution would be the following:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(description))]) {
        [[self detailsOfTask] setText:[object description]];
    } 
}

This will set the label to the description of the cell. Again, this makes no sense but it solves your immediate issue.