我有一个UITableViewCell,并希望将一个自定义对象绑定到它。我知道的唯一选项是创建一个自定义单元格,如下所示:
MyCustomCell *cell = [[MyCustomCell alloc] initWithFrame:<SomeFrame>];
customCell.myProperty = someObject;我很想知道Objective是否提供了其他方法来实现这个目的,而不是创建一个customCell。
在阅读时,我点击了这个文档NSObject valueForUndefinedKey:
我试了一下,应用程序就崩溃了。
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:<SomeFrame>];
[cell setValue:someObject forUndefinedKey:@"SomeObject"];崩溃日志显示:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x5a846c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key SomeObject.'请帮我处理这个。
发布于 2012-02-14 09:19:26
您可以使用关联对象来避免子类。至于setValue:forKey:和setValue:forUndefinedKey:,详细信息请查看头文件(NSKeyValueCoding.h)。
发布于 2012-03-21 13:43:33
当在CustomCell nib的接口生成器中,文件的所有者自定义类设置为CustomCell类时,就会发生此问题。
文件的所有者自定义类应该始终设置为UITableViewCell。表视图对象自定义类应该设置为CustomCell类。示例(NotificationCell是我的自定义单元格类):

发布于 2012-02-14 07:35:51
正如文档中所说的,The default implementation raises an NSUndefinedKeyException.。如果要使用该方法,则必须在自定义类中重写它。
另外,不要对单元格进行bind,这意味着不在单元格对象中保留外部对象。首先,这破坏了MVC模式。其次,您很可能在单元重用方面遇到问题。
https://stackoverflow.com/questions/9272806
复制相似问题