由取消排队导致的崩溃可重用单元格与标识符:对于自定义单元格



更新 - 解决方法

我通过做这些找到了解决方法:(但我仍然不明白为什么在这种情况下我们必须使用dealloc [_profileImage release];,即使我们不拥有,alloc也不是new也不是copy_profileImage

MyUITableView.m

- (void)dealloc {
    [_profileImage release];
    // and all other ivars get released here
    [super dealloc];
}
- (void)onClickLogoutButton {
    if (_profileImage != nil) {
        _profileImage = nil;
    }
    // and other operations
}

当我在onClickLogoutButton中有一个[_profileImage release];时,就会发生崩溃,因为我不拥有(既不拥有(既不alloc也不new也不copy_profileImage,而只是使用_profileImage = [UIImage imageWithData:data];将对象传递给_profileImage

- (void)onClickLogoutButton {
    if (_profileImage != nil) {
        [_profileImage release];
        _profileImage = nil;
    }
    // and other operations
}

原始问题

以下代码在 Xcode 5、iOS 7 中使用手动保留释放 (MRR)。

ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:identifierForProfileCell];导致崩溃,则错误消息之一为Thread 1: EXC_BAD_ACCESS (code=2, address=0x2448c90c)

是因为我错误地发布了一些东西吗?但我不确定何时何地发布什么。项目中有一个注销功能,注销后我们应该发布一些东西还是让 dealloc 来完成这项工作?首次登录,然后注销,然后重新登录,滚动到配置文件单元格并崩溃时,会发生此错误。

我应该在注销后致电MyUITableView.m [self reloadData];吗?

MyUITableView.m

-(UITableViewCell *)tableView:(UITableView *)tableViewLeft cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //ProfileCell
    if ([indexPath section] == 0 && [indexPath row] == 0) {
        static NSString *identifierForProfileCell   = @"ProfileCell";
        ProfileCell *cell = (ProfileCell *)[tableViewLeft dequeueReusableCellWithIdentifier:identifierForProfileCell]; // This line causes crash: Thread 1: EXC_BAD_ACCESS (code=2, address=0x2448c90c)
        if (cell == nil) {
            cell = [[[ProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierForProfileCell] autorelease];
        }
        [[cell textField] setText:_userID];
        if (_profileImage == nil && _profileURL != nil) {
            NSURL *url = [NSURL URLWithString:_profileURL];
            NSURLRequest* request = [NSURLRequest requestWithURL:url];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response, NSData * data, NSError * error) {
                    if (!error) {
                        _profileImage = [UIImage imageWithData:data];
                        [[cell iconView] setImage:_profileImage];
                    }
                }];
            }
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSetting:)];
            [singleTap setNumberOfTapsRequired:1];
            [cell.settingWrapper addGestureRecognizer:singleTap];
            [singleTap release];
            return cell;
        }
    } else {
        // ....
    }
    return nil;
}

你以前在tableView中注册过ProfileCell吗?

像这样在视图DidLoad :

- (void)viewDidLoad
  {
    [super viewDidLoad];
    [self.tableView registerClass:ProfileCell  forCellReuseIdentifier:@"ProfileCell"];
  }

最新更新