2010-11-13 35 views
1

我有一個bissare問題。在一個UITableViewController的viewDidLoad方法,我有:NsMutableArray無理由更改爲UITableViewCellContentView

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' 

我很迷茫。誰能幫我?謝謝。

回答

4

您犯了內存管理錯誤。如果您希望它保留在第一個方法的末尾以外,則必須保留該陣列。

就你的情況而言,數組曾經存在的內存(在被autorelease池釋放之前)被UITableViewCell的子視圖覆蓋。

+0

謝謝你。我應該知道這一點。 – 2010-11-13 01:41:06

1

Ole Begemann很可能找到了問題的原因,但是當您遇到內存管理問題時(發送到錯誤實例的選擇器是內存管理問題的標誌),您可以執行以下操作:

  1. 重新閱讀可可memory management rules並確保您關注他們。
  2. 運行static analyser。這往往會挑選你忽略內存管理規則的地方。
  3. 嘗試使用NSZombieEnabled來確定是否[以及何時]將消息發送給未分配的實例。
+0

感謝您的提示。 Objective-C越來越好,我越來越好了。 – 2010-11-13 01:41:44