I have a bissare problem. In the viewDidLoad method of a UITableViewController I have:
myfields = [NSMutableArray array];
for (int x = 0; x < 10; x++) {
UITextField * field = [[UITextField alloc] initWithFrame:CGRectMake(130,12,150,25)];
field.backgroundColor = [UIColor clearColor];
field.font = [UIFont systemFontOfSize:15];
field.delegate = [[OptionFieldDelegate alloc] init];
[myfields addObject: field];
}
NSLog(@"myfields array - %@",myfields);
That gives this output:
myfields array - (
"<UITextField: 0x6a35b50; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a35c60>>",
"<UITextField: 0x6a36820; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a367f0>>",
"<UITextField: 0x6a374e0; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a374b0>>",
"<UITextField: 0x6a382e0; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a382b0>>",
"<UITextField: 0x6a38ea0; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a38e70>>",
"<UITextField: 0x6a39950; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a39a60>>",
"<UITextField: 0x6a3a630; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a3a600>>",
"<UITextField: 0x6a3b1f0; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a3b1c0>>",
"<UITextField: 0x6a3bdb0; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a3bd80>>",
"<UITextField: 0x6a3c970; frame = (130 12; 150 25); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a3c880>>"
)
That’s fine but the next bit isn’t. I don’t touch that array before I need to, yet in the – tableView:cellForRowAtIndexPath: method I have this:
NSLog(@"Array = %@",myfields);
NSLog(@"class - %@",NSStringFromClass([myfields class]));
NSLog(@"0 - %i",[indexPath row] - 2);
NSLog(@"1 - %@",[myfields objectAtIndex:[indexPath row] - 2]);
[[cell contentView] addSubview: [myfields objectAtIndex:[indexPath row] - 2]];
This is the output:
2010-11-13 00:58:13.808 Poll Maker[1803:207] Array = <UITableViewCellContentView: 0x6a35b10; frame = (10 1; 300 43); layer = <CALayer: 0x6a3e680>>
2010-11-13 00:58:13.809 Poll Maker[1803:207] class - UITableViewCellContentView
2010-11-13 00:58:13.809 Poll Maker[1803:207] 0 - 0
2010-11-13 00:58:13.810 Poll Maker[1803:207] -[UITableViewCellContentView objectAtIndex:]: unrecognized selector sent to instance 0x6a35b10
2010-11-13 00:58:13.812 Poll Maker[1803:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView objectAtIndex:]: unrecognized selector sent to instance 0x6a35b10'
I am very confused. Can anyone help me? THank you.
You made a memory management mistake. You have to retain the array if you want it to stay around beyond the end of the first method.
In your case, the memory where the array once was (before it was released by the autorelease pool) got overwritten with a subview of a UITableViewCell.