gpt4 book ai didi

iphone - cellForRowAtIndexPath 中的崩溃

转载 作者:行者123 更新时间:2023-11-28 17:43:26 27 4
gpt4 key购买 nike

我的应用程序因以下堆栈跟踪而崩溃。我没有在我的 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 在你使用它之前被释放,那么你可能不会取回你期望得到的表格单元格。如果您保留您的 NSString 并在完成后显式释放它,会发生什么情况?像这样...

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

关于iphone - cellForRowAtIndexPath 中的崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7156705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com