2011-07-04 74 views
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"myCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

NSArray *allSections =[self.tableDataSource allKeys]; 
NSArray *aSection = [self.tableDataSource objectForKey:[allSections objectAtIndex:indexPath.section]]; 
NSMutableDictionary *item = [aSection objectAtIndex:indexPath.row] ; 

if (cell == nil) { 

    /* Trace 1 */ 
    NSLog(@"Init: Section: %i - Row: %i", indexPath.section, indexPath.row); 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 

    UISwitch *mySwitch = [[UISwitch alloc] init]; 
    [mySwitch addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventValueChanged]; 
    [mySwitch setOn:NO animated:NO]; 
    cell.accessoryView = mySwitch; 
    cell.accessoryView.tag = [[item objectForKey:@"ID"] integerValue]; 
    cell.textLabel.text = [item objectForKey:@"Name"]; 
    [item setObject:cell forKey:@"cell"]; 

} else { 

    /* Trace 2 */ 
    NSLog(@"Done: Section: %i - Row: %i", indexPath.section, indexPath.row); 
    cell = [item objectForKey:@"cell"]; 

} 

return cell;} 

您好所有,UITableView的運行時墜毀

我有一個UITableView +配件這是崩潰「某個時候」在運行中掙扎。主代碼tableView:cellForRowAtIndexPath就在上面,據我所知,它實際上是基於UITableView的標準模式。

我的dataSource很小,它只是一個2節的數據集,6行之一,另一行只有2行。所有關於段數和段數的方法都被執行。

IB中的用戶界面具有所需的連接,實際上一切正常。 IB中的UITableView略小於主視圖(tableview下面有2個按鈕)。當我運行應用程序時,我可以在視圖中看到前6行(第一部分),但最後2行(第二部分)被隱藏。當然,如果我向下滾動,最後2行將出現...

問題是當我減少IB中的UITableView插座大小。如果插座在運行時僅顯示2或3行,那麼如果向下滾動,應用程序將崩潰。崩潰將發生在第二部分的第一行。只有改變以前的情況是IB插座尺寸,代碼中沒有任何改變。在控制檯

錯誤信息是:

*斷言故障 - [UITableView的_createPreparedCellForGlobalRow:withIndexPath:],/SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678 2011-07-04 02 :08:46.713 iWiR客戶端[95362:207] *終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',理由是: 'UITableView的數據源必須返回從的tableView細胞:的cellForRowAtIndexPath:' 如果你看一下代碼

,你會看到有2個NSLog跟蹤。代碼的正常行爲是:「Trace 1」將顯示在控制檯中用於該行的第一次顯示,然後,如果我上下滾動,那將是「trace 2」。這就是如果UITableView的IBoutlet「足夠大」的情況,但正如我所說的,如果我減小插座的尺寸,控制檯將顯示「跡線2」,即使是該行的第一個顯示。

我希望這些(長)的解釋是不夠清楚,TKS一幫您的幫助...

乾杯, 皮埃爾

回答

1

想象一下,6行的表視圖出其中的2行是可見。當您向下滾動以獲取第4行到可見區域時,您從表視圖中出列的cell將不會是nil,因此將執行else子句。

cell = [item objectForKey:@"cell"]; 

此時沒有爲密鑰cell設置對象。因此objectForKey:將返回nil,並且單元格將爲nil,您將在方法結束時返回。這是因爲您在此處返回nil而導致出現'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'錯誤的原因。

您不通過UITableView提供的機制重複使用單元格。你的方法應該更像,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray *allSections =[self.tableDataSource allKeys]; 
    NSArray *aSection = [self.tableDataSource objectForKey:[allSections objectAtIndex:indexPath.section]]; 
    NSMutableDictionary *item = [aSection objectAtIndex:indexPath.row] ; 
    UITableViewCell *cell = [item objectForKey:@"cell"];  

    if (cell == nil) { 
     NSLog(@"Init: Section: %i - Row: %i", indexPath.section, indexPath.row); 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 

     [..] 
    } 

    return cell; 
} 
+0

你好Deepak,你是100%的權利,我做了改變,當然它效果很好!在我身上感到羞恥......我的目標是關注IB尺寸問題,對代碼沒有足夠的重視...... – pitou75

+0

Tks很多......乾杯,皮埃爾 – pitou75