【问题标题】:UITableViewCell Leak when deleting and saving the cell删除和保存单元格时UITableViewCell泄漏
【发布时间】:2015-11-03 17:34:21
【问题描述】:

我注意到我的一个自定义 UITableViewCell 正在泄漏。执行编辑时不会释放它,删除底部单元格并点击保存。这是我在表格中的顶部单元格(我称之为添加单元格)。一旦我的表格视图处于编辑模式,就会显示添加单元格,下面显示所有其他单元格并启用删除模式。现在,在执行删除操作并保存数据后,即使我的 table view 上的 dealloc 被调用,Add Cell 仍然会挂起。

我附上了我的代码和仪器屏幕截图(我已经从屏幕截图中删除了第一行(Malloc 调用)。对我来说,iOS 内部处理似乎有问题。请告知。

- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
    MyTableViewCell *aCell = nil;
    NSString *aCellType;

    if (iIndexPath.row == 0 && self.inEditMode) {
        aCellType = kMyAddCell;
        aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];

        if (!aCell) {
            aCell = [[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType];
        }

        aCell.isGroupedView = YES;
        aCell.delegate = self;
        aCell.textLabel.text = @“Add More";
        self.tableView.allowsSelectionDuringEditing = YES;
    } else {
        aCellType = kMyDefaultCell;
        aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];

        if (!aCell) {
            aCell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType];
        }

        NSInteger anIndex = iIndexPath.row;

        if (self.inEditMode) {
            anIndex = anIndex - 1;
        }

        aCell.textLabel.text = @“Name";
        aCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return aCell;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)iIndexPath {
    return (iIndexPath.row == 0 && self.inEditMode) ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;
}


- (BOOL)tableView:(UITableView *)iTableView canEditRowAtIndexPath:(NSIndexPath *)iIndexPath {
    return YES;
}


- (void)tableView:(UITableView *)iTableView commitEditingStyle:(UITableViewCellEditingStyle)iEditingStyle forRowAtIndexPath:(NSIndexPath *)iIndexPath {
    if (iEditingStyle == UITableViewCellEditingStyleDelete) {
    [self setSaveModeNavigationBar];
        [self.tableView beginUpdates];
        [self.enabledUsers removeObjectAtIndex:(self.inEditMode) ? iIndexPath.row - 1 : iIndexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[iIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}


- (void)setEditing:(BOOL)iEditing animated:(BOOL)iAnimated {
    [super setEditing:iEditing animated:iAnimated];
    [self.navigationItem setHidesBackButton:YES animated:NO];

    self.headerViewLabel.text = @“Edit Me";

    UIBarButtonItem *aDoneButton = [[UIBarButtonItem alloc] initWithTitle:@“Done"
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector(cancelEditing)];
    [self.navigationItem setRightBarButtonItem:aDoneButton];
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.inEditMode = YES;
    [self.tableView reloadData];
}

点击保存按钮后调用的代码:

    self.requestHandler = [[MyRequestHandler alloc] initWithEndPoint:myEndPoint body:aPostBody successHandler:^(NSDictionary *iResponse) {
        [aBlockSelf addNoDataOverlay];
    } andErrorHandler:^(NSString *iMessage, NSString *iKey, NSInteger iErrorCode, BOOL iIsNetworkError) {
        [aBlockSelf addNoDataOverlay];
    }];

[self.requestHandler executeRequest];

仪器分配堆栈:

【问题讨论】:

    标签: objective-c iphone uitableview cocoa-touch memory-management


    【解决方案1】:

    显然,这是this thread 中跟踪的一个已知问题。在 iPhone 5S (iOS 8) 中尝试了相同的步骤,问题已经解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多