objective-c - 错误-[NSIndexPath 发布] : message sent to deallocated instance 0x6a9d790

标签 objective-c ios ipad uitableview

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

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

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

@end

在我的表格 View Controller 文件中我创建了这个

#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

当用户选择第二行时,我想在 TableView 中添加新行。 为此,我描述了这个方法

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

在 Debug模式下,我可以在单击indexPath.row == 1的行后看到,该 Controller 使用indexPath.row = 1和2调用cellForRowAtIndexPath方法两次。但是After It Controller 调用方法

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

再次调用 cellForRowAtIndexPath 方法,程序出现此错误后(我启用僵尸)错误 -[NSIndexPath release]:消息发送到已释放实例 0x6a9d790

最佳答案

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

关于objective-c - 错误-[NSIndexPath 发布] : message sent to deallocated instance 0x6a9d790,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8952406/

相关文章:

ios - UITableViewController 不显示自定义原型(prototype)单元格

objective-c - 如何查找 UILabel 文本是否缩短/缩小?

iOS 和 Mysql 事件

iphone - 在 iphone/ipad 上测试本地网页

ios - iPad 上的 UIPopoverPresentationController

objective-c - 将 NSInteger 转换为字节

objective-c - NSRunAlertPanel 和超链接

ios - 如何使用 FBSDKLoginButton 注销? (奇怪的错误,因为 Facebook 假设我已登录)

ios - AVFoundation、AVCaptureMetadataOutput 截屏

ios - 我需要为 uitableview 性能 ipad 做更多的优化