I have a UITableViewCell and want to bind a custom object to it. The only option I know is to create a custom cell like:
MyCustomCell *cell = [[MyCustomCell alloc] initWithFrame:<SomeFrame>];
customCell.myProperty = someObject;
I’m curious to know if Objective C provides any other way to do this instead of creating a customCell.
While reading, I hit this document NSObject valueForUndefinedKey:
I tried the following and the app crashed.
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:<SomeFrame>];
[cell setValue:someObject forUndefinedKey:@"SomeObject"];
Crash Log Shows:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x5a846c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key SomeObject.'
Please help me with this.
You can use associated objects to avoid subclassing. As for the
setValue:forKey:andsetValue:forUndefinedKey:, take a look in the header file (NSKeyValueCoding.h) for details.