2013-11-27 32 views
1

我在我的代碼中遇到了一個奇怪的崩潰。一個奇怪的UITableView崩潰:NSRangeException

下面的代碼:發生在代碼的最後一行

UIView *tableHeaderView = self.tableView.tableHeaderView; 
CGRect frame = tableHeaderView.frame; 
frame.size.height = 0; 
tableHeaderView.frame = frame; 
self.tableView.tableHeaderView = tableHeaderView; 

崩潰。

而且,這裏是日誌

Terminating app due to uncaught exception 'NSRangeException', reason: 
'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 
*** First throw call stack: 
(0x345ae2a3 0x3c25397f 0x344f9b75 0x1a7055 0x363ebb8f 0x363d702b 0x363d7599 0x363d740d 0x3642b22b 0x1a0d77 0x363d5595 0x3642a14b 0x3642a091 0x36429f75 0x36429e99 0x364295d9 0x364294c1 0x364764d1 0x36428861 0x36428713 0x364a4399 0x364508fb 0x36691619 0x364a39b9 0x364a1fe7 0x26c8a1 0x221997 0x34ee96fd 0x34e291f9 0x34e29115 0x3428b45f 0x3428ab43 0x342b2fcb 0x344f474d 0x342b342b 0x3421703d 0x34583683 0x34582ee9 0x34581cb7 0x344f4ebd 0x344f4d49 0x380b82eb 0x3640a301 0x10819f 0x3c68ab20) 
libc++abi.dylib: terminate called throwing an exception 

我真的沒有任何想法發生了什麼我的代碼。

任何建議對我很有幫助!

謝謝。

+1

以上提供的代碼不是此例外的原因。 –

+2

在cellforrow和viewforheader方法中放置一些NSLogs以查找崩潰的確切位置。 – NAZIK

+1

您可以粘貼cellforRowAtIndexpath方法代碼,或只是檢查您傳遞給tableview的數組是否爲空或有一些數據? – SRI

回答

1

你一定初始化在類中的一些陣列放置的NSLog首先檢查你的陣列,它應該有有些值或它爲空。我認爲這是空的,這就是爲什麼他們造成一個問題。

0

如果你想改變headerView的表,這是正確的方式做到這一點:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *headerView = [[UIView alloc] initWithFrame:kHeaderFrame]; 
    return headerView; 
} 
+2

@DuricanRadhu在他的問題中,他想設置「tableHeaderView」,而不是每個節標題視圖。 –