NsMutableArray无缘无故地更改为UITableViewCellContentView

时间:2010-11-13 01:05:38

标签: iphone objective-c ios nsmutablearray

我有一个bissare问题。在UITableViewController的viewDidLoad方法中,我有:

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);

这给出了这个输出:

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>>"
)

那很好,但下一点不是。我不需要之前触摸那个数组,但是在 - tableView:cellForRowAtIndexPath:方法中我有这个:

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]];

这是输出:

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'

我很困惑。谁能帮我?谢谢你。

2 个答案:

答案 0 :(得分:4)

你犯了内存管理错误。如果你希望它保持在第一种方法的末尾,你必须保留数组。

在您的情况下,数组曾经存在的内存(在自动释放池发布之前)被UITableViewCell的子视图覆盖。

答案 1 :(得分:1)

Ole Begemann可能找到了问题的原因,但是当你遇到内存管理问题时(选择器被发送到错误的实例是内存管理问题的一个标志),你可以做很多事情:

  1. 重新阅读Cocoa memory management rules并确保您关注它们。
  2. 运行static analyser。这通常会占用您忽略内存管理规则的地方。
  3. 尝试使用NSZombieEnabled查看是否[以及何时]向未分配的实例发送邮件。