gpt4 book ai didi

ios - 发送到实例的 UITableViewCell copyWithZone 无法识别的选择器

转载 作者:行者123 更新时间:2023-11-28 22:04:40 24 4
gpt4 key购买 nike

我正在尝试使用 KVO 更新我的自定义 tableviewcell (DHtableViewCell) 中的值。我不断收到此错误。我知道还有其他人也有同样的异常(exception),但他们的解决方案没有帮助。

-[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?我该如何解决这个问题?

最佳答案

问题是 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。同样,这没有任何意义,但它可以解决您眼前的问题。

关于ios - 发送到实例的 UITableViewCell copyWithZone 无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24317091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com