UITableViewCellAccessibilityElement 发布在选项卡切换时崩溃
Posted
技术标签:
【中文标题】UITableViewCellAccessibilityElement 发布在选项卡切换时崩溃【英文标题】:UITableViewCellAccessibilityElement release Crashes on Tab Switching 【发布时间】:2012-10-18 13:14:33 【问题描述】:我正在为 ios(5 以上)开发一个选项卡式应用程序。 在 3 个选项卡中的 2 个选项卡之间进行几次切换后,我的应用程序在模拟器中遇到崩溃,这两个选项卡都包含表(以及其他一些 - 这里可能不相关的东西)。以下是单元格创建的重要部分和错误代码。
崩溃发生在(每次不同的)更改次数之后,并且总是在访问选项卡 2 时发生。
注意我正在使用 ARC。
* UPDATE* 该问题仅出现在 iOS 6 上。以前的版本不受影响。它可能依赖于一些被调用的线程代码,如下所示:
if (!self.progressBackground)
self.progressBackground = [[UIView alloc]initWithFrame:CGRectMake(100, 260, 120, 120)];
[self.progressBackground setBackgroundColor:[UIColor blackColor]];
[[self.progressBackground layer]setCornerRadius:15];
UILabel *progressText = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 120, 30)];
UIFont *fontName = [UIFont fontWithName:@"Helvetica" size:14.0];
[progressText setFont:fontName];
[progressText setText:@"Updating Tags"];
[progressText setTextColor:[UIColor whiteColor]];
[progressText setTextAlignment:UITextAlignmentCenter];
[progressText setBackgroundColor:[UIColor clearColor]];
[self.progressBackground addSubview:progressText];
[self.progressBackground setAlpha:.6];
if (!self.progressView)
self.progressView = [[UIActivityIndicatorView alloc]initWithFrame:self.progressBackground.frame];
[self.progressView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:self.progressBackground];
[self.view addSubview:self.progressView];
[self.progressView startAnimating];
[NSThread detachNewThreadSelector:@selector(initializeCategoriesArray) toTarget:self withObject:nil];
'initializeCategoriesArray' 方法看起来像这样:
-(void)initializeCategoriesArray
if (!self.categoryArchive)
self.categoryArchive = [[NSMutableArray alloc]init];
//set the right month' expenses
self.currentMonthExpenses = (NSMutableArray*)[self.dataHandler fetchAllExpensesForMonth:[NSNumber numberWithInteger:self.currentMonthNumber ]];
// check if expenses exist at all
if (self.currentMonthExpenses.count == 0)
[self.categoryArchive removeAllObjects];
[self.categoriesTable reloadData];
[self.progressView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
[self.progressView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
[self.progressBackground performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
return;
// sort out month Expenses
NSMutableArray *dayTypeExpenses = [[NSMutableArray alloc]init];
for (Expense *exp in self.currentMonthExpenses)
if (exp.expenseType.boolValue == NO)
[dayTypeExpenses addObject:exp];
// check if dayExpenses exist
if (dayTypeExpenses.count == 0)
[self.categoryArchive removeAllObjects];
[self.categoriesTable reloadData];
[self.progressView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
[self.progressView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
[self.progressBackground performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
return;
// create Dictionary holding all DayExpenses under their STRING AS KEY
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
for (Expense *exp in dayTypeExpenses)
NSMutableArray *temp = [tempDict objectForKey:exp.name];
if (temp == nil)
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
[tempDict setValue:tempArray forKey:exp.name];
[[tempDict objectForKey:exp.name]addObject:exp];
// create temporary (unsorted) array of Categories
NSMutableArray *tempCatArray = [[NSMutableArray alloc]init];
for (NSMutableArray *expArray in [tempDict allValues])
double totValue = 0;
NSInteger count = 0;
double average = 0;
CGFloat percentage = 0;
for (Expense* exp in expArray)
count++;
totValue = totValue+exp.value.doubleValue;
average = totValue/count;
percentage = totValue/self.spentCurrentMonth;
Category *newCategory = [[Category alloc]init];
[newCategory setTotalValue:totValue];
[newCategory setName:[[expArray objectAtIndex:0]name]];
[newCategory setNumberOfExpenses:count];
[newCategory setAverageValue:average];
[newCategory setPercentOfBudgetInFloat:fabs(percentage)];
[tempCatArray addObject:newCategory];
// sort tempArray
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"totalValue" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
self.categoryArchive = [[tempCatArray sortedArrayUsingDescriptors:sortDescriptors]mutableCopy];
// find normfactor
CGFloat normFactor = fabs(1.0/[[self.categoryArchive objectAtIndex:0] percentOfBudgetInFloat]);
for (Category *cat in self.categoryArchive)
if (cat.name.length == 0)
cat.name = @"Uncategorized";
if (cat.percentOfBudgetInFloat >= 1)
cat.percentOfBudgetInFloat = 1;
cat.percentOfBudgetInFloat = cat.percentOfBudgetInFloat*normFactor;
// reload
[self.categoriesTable reloadData];
[self.progressView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
[self.progressView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
[self.progressBackground performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
标签 1:
TableView, each cell contains 2 labels 1 button
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"dayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
UIFont *subjectFont = [UIFont fontWithName:@"Helvetica" size:14.0];
UIFont *valueFont = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[cell.textLabel removeFromSuperview];
// Cell Look
UIImageView *backgroundImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell" ]];
[cell setBackgroundView:backgroundImg];
// Subjectlabel
UILabel *subjectLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 15, 160, 20)];
//...
// ValueLabel
UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 15, 90, 20)];
//...
// Deletion Button
UIButton *delButton = [[UIButton alloc]initWithFrame:CGRectMake(255, 0, 50, 50)];
[delButton setBackgroundColor:[UIColor clearColor]];
[delButton setImage:[UIImage imageNamed:@"delButt" ] forState:UIControlStateNormal ];
delButton.tag = 1002;
[cell addSubview:delButton];
//...
UIButton *delButton = (UIButton*)[cell viewWithTag:1002];
[delButton addTarget:self action:@selector(deleteRow:) forControlEvents:UIControlEventTouchUpInside];
//...
return cell;
标签 2:
TableView, each cell contains 2 labels, 1 rectangular UIView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"thisMonthCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
UIFont *subjectFont = [UIFont fontWithName:@"Helvetica" size:14.0];
UIFont *valueFont = [UIFont fontWithName:@"Helvetica-Bold" size:16.0];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[cell.textLabel removeFromSuperview];
// BarGraph for this Cell
UIView *barProgress = [[UIView alloc]initWithFrame:CGRectMake(20, 1, 1, 37)];
[barProgress setBackgroundColor:[self goldBarColor]];
[barProgress setAlpha:.5];
barProgress.tag = 1000;
[cell addSubview:barProgress];
Category *cellCategory = [self.categoryArchive objectAtIndex:indexPath.row];
UIView *progressBar = (UIView *)[cell viewWithTag:1000];
[progressBar setFrame:CGRectMake(progressBar.frame.origin.x, progressBar.frame.origin.y, cellCategory.percentOfBudgetInFloat*280, progressBar.frame.size.height)];
//...
return cell;
崩溃时的错误代码如下:
2012-10-18 14:57:13.218 DailyBudget[4714:c07] * -[UITableViewCellAccessibilityElement release]:消息发送到已释放实例 0x7571630 dlopen(/Applications/Xcode.app/Contents/PlugIns/DebuggerFoundation.ideplugin/Contents/Resources/DebuggerIntrospectionSupport.dylib,0x00000002) dyld:加载:/Applications/Xcode.app/Contents/PlugIns/DebuggerFoundation.ideplugin/Contents/Resources/DebuggerIntrospectionSupport.dylib
问题仅出现在模拟器中,而不出现在真实设备上(均为 iOS 6.0)
任何想法可能是什么问题?
【问题讨论】:
【参考方案1】:编辑:
实际问题是从主线程更新 UI。阅读 cmets 了解详情。
--- 原始答案---
我建议您使用自定义单元格,而不是尝试以编程方式更改内置的 Apple tableviewcell。文档有一些很好的例子来说明如何做到这一点。如果你有故事板,那真的很容易。
我的猜测是你弄乱了 tableview 单元格的对象(文本标签 -- 附件视图连接)中的预期保留计数发生了一些事情,并且 arc 没有捕捉到你的 removeFromSuperview 魔法。
【讨论】:
这不是问题所在。似乎遗留线程代码使它跳跃。我删除了背景线程并现在在主线程中进行计算 - 奖励仍然存在,因为我真的不知道为什么这是一个问题;) 所有 ui 代码都需要在主线程上完成。对不起,我错过了。 只有一行 [_tableView reloadData],所以这是唯一的问题? 应该是。打开 XCode,打开文档并粘贴这个threads and your user interface
这个小简介应该比我能更好地解释它。 :-)【参考方案2】:
UIKit 可能会尝试发布一些它内部预计不需要发布的东西。尝试在这两个实现中注释掉 [cell.textLabel removeFromSuperview];
并将其替换为 cell.textLabel.hidden = YES
,这很可能会阻止这次崩溃。
【讨论】:
感谢输入,遗憾的是这并没有解决问题:(以上是关于UITableViewCellAccessibilityElement 发布在选项卡切换时崩溃的主要内容,如果未能解决你的问题,请参考以下文章