2012-01-21 9 views
0

カスタムセルでtableViewを作成します。ここ 私はこのエラー - [NSIndexPath release]:メッセージが割り当て解除されたインスタンスに送信されました。0x6a9d790

#import <UIKit/UIKit.h> 
#import "ZUITableViewCell.h" 

@protocol ZUITableViewControllerDelegate ///for main view controller 

-(void) doneButtonPressed; 

@end 

@interface ZUITableViewController : UITableViewController { 
    id <ZUITableViewControllerDelegate> delegate; 

    NSMutableArray *menuItems; 
    BOOL isAddingFields; 
} 

@property (nonatomic, retain) NSArray *menuItems; 
@property (nonatomic, assign) id<ZUITableViewControllerDelegate> delegate; 

- (CGRect) GetCellHegiht: (ZUITableViewCell *) cell; 

@end 

私はユーザが第2行を選択したときに、私のテーブルビューに新しい行を追加したい作成.hファイル

@interface ZUITableViewCell : UITableViewCell { 
    IBOutlet UITextField *_textFieldOutlet; 
    IBOutlet UILabel *_textLabelOutlet; 

} 
@property (retain, nonatomic) IBOutlet UITextField *_textFieldOutlet; 
@property (retain, nonatomic) IBOutlet UILabel *_textLabelOutlet; 

@end 

そして、私のテーブルビューコントローラファイル内のセルのコード。これは私がこの方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
    */ 
    NSLog (@"row = %d", indexPath.row); 
    NSLog(@"INIT %p", self); 
    ZUITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell._textFieldOutlet becomeFirstResponder]; 
    if (indexPath.row == 1 && isAddingFields == FALSE) { 
     isAddingFields = TRUE; 
     [menuItems insertObject:@"CC" atIndex:1]; 
     [menuItems insertObject:@"BCC" atIndex:2]; 

     NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0]; 
     NSIndexPath *path2 = [NSIndexPath indexPathForRow:2 inSection:0]; 
     NSArray *indexArray = [NSArray arrayWithObjects:path1,path2,nil]; 
     [tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop]; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     [path1 release]; 
     [path2 release]; 

     [cell._textFieldOutlet becomeFirstResponder]; 
    } 
} 

を説明し、私はindexPath.row == 1での行にこのコントローラのコールをクリックした後に見ることができるように私のcellForRowAtIndexPathメソッドデバッグモードで

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    NSLog(@"INIT %p", self); 
    NSLog (@"cell for row = %d", indexPath.row); 
    ZUITableViewCell *cell = (ZUITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelsObjects = [[NSBundle mainBundle] loadNibNamed:@"ZUITableViewCell" owner:nil options:nil]; 
     for (id currentObject in topLevelsObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (ZUITableViewCell *) currentObject; 
       break; 
      } 
     } 
    } 
    if (indexPath.row == 3 && isAddingFields == FALSE){ 
     // [cell setBounds:[self GetCellHegiht:cell]]; 
    } 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell._textLabelOutlet.text = [menuItems objectAtIndex:indexPath.row]; 
    cell._textFieldOutlet.tag = indexPath.row; 
    [cell._textFieldOutlet setEnabled:YES]; 
    if(indexPath.row == 0){ 
     [cell._textFieldOutlet becomeFirstResponder]; 
    } 


    return cell; 
} 

があるようにするに indexPath.row = 1と2と方法のcellForRowAtIndexPath 2回しかしそれは、callメソッドを再度

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//#warning Incomplete method implementation. 
    return [menuItems count]; 
} 

及び方法cellForRowAtIndexPath CAをコントローラ後(NSIndexPathリリース):メッセージが割り当て解除されたインスタンスに送信されました。0x6a9d790

答えて

1

[NSIndexPath indexPathForRow:inSection:](パス1とパス2で使用される)メソッドは、自動解放されたオブジェクトを返します。コールする必要はありません後にrelease

+0

ああ、ありがとう。できます。 – nabiullinas

+0

ようこそ。 –

関連する問題