为什么我的应用程序崩溃? UITable和控制器
我有这个没有错误的大表,即使在我看来我太慢了,比我从下载的例子打开更复杂的单元格。问题是当我点击一个单元格,然后回来,在滚动快速的应用程序是在该行崩溃的几秒钟:charImage.image = [self imageForIndex:ch.charId];
为什么我的应用程序崩溃? UITable和控制器
如果我删除其下一行崩溃:titleLabel.text = ch.title;
是谁熟悉这个?谢谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"add a new cell at index path %@", indexPath);
// Each subview in the cell will be identified by a unique tag.
static NSUInteger const kTitleLabelTag = 2;
static NSUInteger const kCharImageTag = 3;
static NSString *kCellID = @"CellID";
// Configure the cell
// Declare references to the subviews which will display the char data.
UILabel *titleLabel = nil;
UIImageView *charImage = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
// No reusable cell was available, so we create a new cell and configure its subviews.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell setBackgroundColor:[UIColor clearColor]];
//
charImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"glyphs/a/1.png"]] autorelease];
charImage.tag = kCharImageTag;
charImage.autoresizingMask = UIViewAutoresizingNone;
[cell.contentView addSubview:charImage];
titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(60, 10, 215, 20)] autorelease];
titleLabel.tag = kTitleLabelTag;
titleLabel.font = [UIFont systemFontOfSize:16];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.adjustsFontSizeToFitWidth = YES;
[cell.contentView addSubview:titleLabel];
} else {
// A reusable cell was available, so we just need to get a reference to the subviews using their tags.
charImage = (UIImageView *)[cell.contentView viewWithTag:kCharImageTag];
titleLabel = (UILabel *)[cell.contentView viewWithTag:kTitleLabelTag];
}
// Get the specific info for this row.
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
Char *ch = (Char *)[charsList objectAtIndex:row+section*26];
// Set the relevant data for each subview in the cell.
// Set the image in the cell
charImage.image = [self imageForIndex:ch.charId];
[charImage sizeToFit];
int centerX = 30;
int centerY = 23;
CGSize size = charImage.frame.size;
charImage.frame = CGRectMake((int)(centerX - size.width/2),
(int)(centerY - size.height/2),
size.width, size.height);
titleLabel.text = ch.title;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
InstantInsightAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
Char *ch = (Char *)[charsList objectAtIndex:row+26*section];
SqlQueries *db = [SqlQueries alloc];
NSArray *glyphsList = [db readGlyphsFromDatabaseAtId: ch.charId];
NSArray *charDescription = [db readMoreInfoFromDatabaseAtId: ch.charId];
Char *charDetails = [db readInstructionsAndQuotesFromDatabaseAtId: ch.charId];
[db release];
[ch release];
// Navigation logic -- create and push a new view controller
self.glyphsView = [[GlyphsViewController alloc] initWithStyle:UITableViewStyleGrouped];
self.glyphsView.glyphsList = glyphsList;
self.glyphsView.charDetails = charDetails;
self.glyphsView.charDescription = charDescription;
[delegate.navController pushViewController:self.glyphsView animated:YES];
[self.glyphsView release];
self.glyphsView = nil;
}
下面是详细的错误:它并没有说什么,但我敢肯定,我已经看到了一些有例外
[会话开始于2009-10-05 07:41:25 +0300。] GNU gdb 6.3.50-20050815(Apple版本gdb-967)(Tue 7月14日02:11:58 UTC 2009) 版权所有2004*软件基金会,公司 GDB是*软件,涵盖了GNU通用公共许可证,并且您是 欢迎在确定下改变它和/或分发它的副本条件。 输入“show copying”查看条件。 GDB绝对没有保修。请输入“显示保修”以了解详情。 这GDB被配置为“I386的苹果达尔文” .sharedlibrary应用负载规则所有附加 处理96464. (GDB)^ C 其中
#0 0x95fd5688 in objc_msgSend()
#1 0x00002f60 in -[IndexViewController tableView:cellForRowAtIndexPath:] (self=0xd19610, _cmd=0x319a6b9c, tableView=0x101be00, indexPath=0xd2e890) at /Users/Cristi/Documents/xcode - iPhone/Instant Insight/InstantInsight/Classes/IndexViewController.m:115
#2 0x30944bd0 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]()
#3 0x3093e1fc in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:]()
#4 0x309501f1 in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow]()
#5 0x30947715 in -[UITableView layoutSubviews]()
#6 0x00b3dd94 in -[CALayer layoutSublayers]()
#7 0x00b3db55 in CALayerLayoutIfNeeded()
#8 0x00b3d3ae in CA::Context::commit_transaction()
#9 0x00b3d022 in CA::Transaction::commit()
#10 0x00b452e0 in CA::Transaction::observer_callback()
#11 0x30245c32 in __CFRunLoopDoObservers()
#12 0x3024503f in CFRunLoopRunSpecific()
#13 0x30244628 in CFRunLoopRunInMode()
#14 0x32044c31 in GSEventRunModal()
#15 0x32044cf6 in GSEventRun()
#16 0x309021ee in UIApplicationMain()
#17 0x00002718 in main (argc=1, argv=0xbfffee50) at /Users/Cristi/Documents/xcode - iPhone/Instant Insight/InstantInsight/main.m:14
什么样的“崩溃”的? EXC_BAD_ACCESS
(取消引用不指向有效位置的指针)?或某种objc_exception_throw
?
假设你的意思是一个EXC_BAD_ACCESS
...
这是因为从[charsList objectAtIndex:row+section*26]
返回的“CH”值不是一个有效的对象。
您需要发现自己的确切原因,但最可能的原因是您已经多次释放对象。当它插入到NSArray中时,对象也可能不是有效的Objective-C对象。
有时它有助于设置NSZombieEnabled(命令选项-x,转到参数选项卡并添加名称= NSZombieEnabled和值= 1到环境变量),因为如果您尝试使用释放目的。
-
无关文体点:你分配SqlQueries但不进行初始化。如果你alloc
,总是,总是init
。即[[SqlQueries alloc] init]
。如果你不关心init
,因为你没有任何实例变量,那么你不应该有alloc
,你的方法应该是类方法。
谢谢你的详细解释。我想我可以把东西放到SqlQueries的init中。 现在,我敢肯定,我的错误是一个例外,因为那里有我试图抓住的想法,可悲的是仍然崩溃,没有调用catch的nslog。 我不知道还有什么可以告诉我的代码。 – 2009-10-04 21:33:25
您无法捕捉到EXC_BAD_ACCESS。你需要弄清楚为什么charList中的对象是无效的。在调试器或NSLog中定期使用断点来定时查看错误(一旦charList对象变为无效,NSLog将使程序崩溃)。 有可能是你的代码在释放(或自动释放)其中一个Char对象时,它从未分配过或保留它 - 这导致Char对象失效,即使它仍然是charList所需要的阵列。 – 2009-10-05 05:25:01
所以,是的,我仔细看了看,看看我是如何释放数组中的Char对象。答案是删除这一行:[ch release]; 再次感谢。 – 2009-10-05 18:38:08
Char * ch =(Char *)[charsList objectAtIndex:row + section * 26];我看起来很奇怪。你把什么样的对象放在charsList中? – diederikh 2009-10-03 12:15:28
我正在放置Char对象。 Char仅包含少量从数据库填充的NSString。如果我只是NSLog这个值,他们很好,即使在崩溃的时候。 – 2009-10-03 18:21:04