2009-10-03 66 views
5

我有這個沒有錯誤的大表,即使在我看來我太慢了,比我從下載的例子打開更復雜的單元格。問題是當我點擊一個單元格,然後回來,在滾動快速的應用程序是在該行崩潰的幾秒鐘: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 
+0

Char * ch =(Char *)[charsList objectAtIndex:row + section * 26];我看起來很奇怪。你把什麼樣的對象放在charsList中? – diederikh 2009-10-03 12:15:28

+0

我正在放置Char對象。 Char僅包含少量從數據庫填充的NSString。如果我只是NSLog這個值,他們很好,即使在崩潰的時候。 – 2009-10-03 18:21:04

回答

5

什麼樣的「崩潰」的? 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,你的方法應該是類方法。

+0

謝謝你的詳細解釋。我想我可以把東西放到SqlQueries的init中。 現在,我敢肯定,我的錯誤是一個例外,因爲那裏有我試圖抓住的想法,可悲的是仍然崩潰,沒有調用catch的nslog。 我不知道還有什麼可以告訴我的代碼。 – 2009-10-04 21:33:25

+0

您無法捕捉到EXC_BAD_ACCESS。你需要弄清楚爲什麼charList中的對象是無效的。在調試器或NSLog中定期使用斷點來定時查看錯誤(一旦charList對象變爲無效,NSLog將使程序崩潰)。 有可能是你的代碼在釋放(或自動釋放)其中一個Char對象時,它從未分配過或保留它 - 這導致Char對象失效,即使它仍然是charList所需要的陣列。 – 2009-10-05 05:25:01

+0

所以,是的,我仔細看了看,看看我是如何釋放數組中的Char對象。答案是刪除這一行:[ch release]; 再次感謝。 – 2009-10-05 18:38:08

相關問題