我正在尝试使用 KVO 更新我的自定义 tableviewcell (DHtableViewCell) 中的值。我不断收到此错误。我知道还有其他人有同样的例外,但他们的解决方案没有帮助。
-[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
实现 tableView:cellForIndexPath 的类设置了一个名为 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!!!!
}
}
所以代码的工作方式是,在 tableview:cellForIndexpath 方法中我设置了 cell.description 属性。然后观察者看到值已经改变然后它去更新 UILabel 对应的文本。
我难住了。为什么它试图调用copyWithZone?我该如何解决这个问题?