【问题标题】:Error -[NSIndexPath release]: message sent to deallocated instance 0x6a9d790错误 -[NSIndexPath release]:消息发送到已释放实例 0x6a9d790
【发布时间】:2012-01-21 10:50:50
【问题描述】:

我使用自定义单元格创建 tableView。 这里是单元格.h文件的代码

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

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

@end

在我的 tableview 控制器文件中我创建了这个

#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

当用户选择第二行时,我想在我的表格视图中添加新行。 为了做到这一点,我描述了这种方法

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

还有我的 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 的行后,此控制器调用方法 cellForRowAtIndexPath 两次,indexPath.row = 1 和 2。但在它之后控制器调用方法

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

再次调用 cellForRowAtIndexPath 方法,在程序执行此错误后(我启用僵尸)错误 -[NSIndexPath release]: message sent to deallocated instance 0x6a9d790

【问题讨论】:

    标签: objective-c ios ipad uitableview


    【解决方案1】:

    方法[NSIndexPath indexPathForRow:inSection:](用于path1和path2)返回一个自动释放的对象,之后不需要调用release

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多