簡體   English   中英

cellForRowAtIndexPath中崩潰

[英]Crash in cellForRowAtIndexPath

以下堆棧跟蹤使我的應用程序崩潰。 我沒有在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)

我先前的答案是錯誤的,但是在更仔細地研究問題時,我注意到您正在使用+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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM