开发者

Crash in cellForRowAtIndexPath

I am getting a crash in my application with following stack trace. I am not inserting any object inside the array in my cellForRowAtIndexPath and still can see this in the logs.

- (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)


My previous attempt at an answer was wrong, but on looking at the problem more closely, I notice that you're creating your cell identifiers using +NSString stringWithFormat:, which returns an autoreleased string that you're not retaining:

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

If that NSString gets deallocated before you use it, then you probably won't be getting back the table cell you expect to get. What happens if you retain your NSString and explicitly release it when you're done with it? Like so...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜