【问题标题】:UITableViewCell copyWithZone unrecognized selector sent to instanceUITableViewCell copyWithZone 无法识别的选择器发送到实例
【发布时间】:2014-06-19 22:14:52
【问题描述】:

我正在尝试使用 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?我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c uitableview key-value-observing copywithzone


    【解决方案1】:

    问题是对observeValueForKeyPath:ofObject:change:context: 的调用中的object 参数是您的单元格实例。然后,您只需将单元实例转换为NSString。标签试图复制字符串。由于对copy 的调用是在单元实例上而不是在某些NSString 上,因此您会遇到崩溃。

    虽然我看不到监听单元描述更改的意义,但一种解决方案如下:

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

    这会将标签设置为单元格的description。同样,这没有任何意义,但它解决了您的直接问题。

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多