Because NSCopying
is not so great at deep (recursive) copies of object graphs. For instance, [NSArray copy]
copies the list of objects, not the objects themselves. Object graphs are better served by NSCoding
. Which in a happy coincidence is supported by UIView
.
If you want to copy custom view with properties, you'll have to support NSCoding
. E.g.,
@interface SKCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel* nameLabel;
@property (strong, nonatomic) IBOutlet UIView* topView;
@end
static NSString* propertiesKey = @"SKCustomCellProperties";
@implementation SKCustomCell
@synthesize nameLabel;
@synthesize topView;
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder: aDecoder];
[self setValuesForKeysWithDictionary: [aDecoder decodeObjectForKey: propertiesKey]];
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder: aCoder];
[aCoder encodeObject: [self dictionaryWithValuesForKeys: [[NSArray alloc] initWithObjects: @"nameLabel", @"topView", nil] forKey: propertiesKey];
}
@end