在cellForRowAtIndexPath中崩溃

时间:2011-08-23 05:38:12

标签: iphone ios cocoa-touch uitableview

我的应用程序崩溃,跟踪堆栈跟踪。我没有在我的cellForRowAtIndexPath中的数组中插入任何对象,仍然可以在日志中看到这一点。

- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {  
MyTableViewCell *aCell = nil;
NSString *aCellType = nil;
if (!self.isEditMode){
    if (iIndexPath.row < [self.locationList count]) {
        aCellType = [NSString stringWithFormat:@"%d", DefaultCell];
        aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
        if (!aCell) {
            aCell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
        }
        aCell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSDictionary *aLocationData = [self.locationList objectAtIndex:iIndexPath.row];
        aCell.textLabel.text = [aLocationData stringForKey:@"locations"];
        aCell.textLabel.accessibilityLabel = [NSString stringWithFormat:@"%@ %@", @"My label", 
                                              [aLocationData stringForKey:@"location"]];
    } 
} else {
    if (iIndexPath.row == 0 && self.isEditMode) {
        aCellType = [NSString stringWithFormat:@"%d", AddCell];
        aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
        if (!aCell) {
            aCell = [[[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
        }
        aCell.selectionStyle = UITableViewCellSelectionStyleBlue;
        aCell.isGroupedView = YES;
        aCell.delegate = self;
        [aCell.actionButton setImage:[UIImage imageNamed:@"My.png"] forState:UIControlStateNormal];
        aCell.textLabel.text = @"Data";
        aCell.textLabel.accessibilityHint = [NSString stringWithFormat:@"%@ %@", @"My Data", 
                                             @"Location"];
    } else if (iIndexPath.row < [self.locationList count] + 1) {
        aCellType = [NSString stringWithFormat:@"%d", DefaultCell];
        aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
        if (!aCell) {
            aCell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
        }
        aCell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSDictionary *aLocationData = [self.locationList objectAtIndex:iIndexPath.row - 1];
        aCell.textLabel.text = [aLocationData stringForKey:@"locations"];
        aCell.textLabel.accessibilityLabel = [NSString stringWithFormat:@"%@ %@", @"My Data", 
                                              [aLocationData stringForKey:@"Location"]];
    }
}

return aCell;

}

0   libsystem_kernel.dylib          0x364aca1c __pthread_kill + 8
1   libsystem_c.dylib               0x361db3b4 pthread_kill + 52
2   libsystem_c.dylib               0x361d3bf8 abort + 72
3   libstdc++.6.dylib               0x33b81a64 __gnu_cxx::__verbose_terminate_handler() + 376
4   libobjc.A.dylib                 0x33c2c06c _objc_terminate + 104
5   libstdc++.6.dylib               0x33b7fe36 __cxxabiv1::__terminate(void (*)()) + 46
6   libstdc++.6.dylib               0x33b7fe8a std::terminate() + 10
7   libstdc++.6.dylib               0x33b7ff5a __cxa_throw + 78
8   libobjc.A.dylib                 0x33c2ac84 objc_exception_throw + 64
9   CoreFoundation                  0x349a1ef6 -[__NSArrayM insertObject:atIndex:] + 466
10  CoreFoundation                  0x349a1d14 -[__NSArrayM addObject:] + 28
11  MyApp                           0x0005c520 -[MyController tableView:cellForRowAtIndexPath:] (MyController.m:268)

1 个答案:

答案 0 :(得分:0)

我之前尝试回答是错误的,但是在更仔细地查看问题时,我注意到您正在使用+NSString stringWithFormat:创建单元格标识符,它会返回您未保留的自动释放字符串:

    aCellType = [NSString stringWithFormat:@"%d", AddCell];
    aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
    if (!aCell) {
        aCell = [[[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
    }

如果NSString在您使用之前被取消分配,那么您可能无法获得预期获得的表格单元格。如果retain你的NSString并在你完成它后明确释放它会怎样?像这样......

    aCellType = [[NSString stringWithFormat:@"%d", AddCell] retain];
    aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
    if (!aCell) {
        aCell = [[[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
    }
    [aCellType release];
相关问题