2011-07-09 61 views
0

當我運行我的ios應用程序時,只有第一個單元格顯示在TableView中,然後單擊時顯示第二個單元格。我希望他們都能同時顯示,但我無法弄清楚這種行爲。第二個單元格僅在Tableview中選擇第一個單元格後出現ios

這裏是我的看法代碼做負載

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"ProductCellId"; 

    ProductTableCell *cell = 
     (ProductTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"ProductTableCell" owner:self options:nil]; 
     cell = self.productCell; 
    }  

    Product *product = [products objectAtIndex:indexPath.row]; 
    [cell configureForProduct:product]; 

    return cell; 
} 

任何幫助,將不勝感激!

回答

0

由於要裝載從界面生成你的試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductTableCell"]; 
     if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ProductTableCell" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 


Product *product = [products objectAtIndex:indexPath.row]; 
[cell configureForProduct:product]; 


    return cell; 
} 

更改此:

static NSString *CellIdentifier = @"ProductCellId"; 

到:

static NSString *CellIdentifier = @"ProductTableCell"; 

你可以找到更多的信息,你正試圖在這個問題上做:Load cells from XIB files

+0

感謝您的迴應。當我的(cell == nil)條件有這個{[[NSBundle mainBundle] loadNibNamed:@「ProductTableCell」owner:self options:nil]; cell = self.productCell; }它做我描述的行爲。當我把(cell == nil){cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; }該應用程序崩潰。還有什麼建議?再次感謝! – mikez

+0

@Mikez爲什麼應用程序崩潰,錯誤是什麼?我假設ProductTableCell是一個UITableView子類,因此更具體的你必須做[[[ProductTableCell alloc],這可能是它崩潰的原因,因爲它會因dequeu上的ClassCastException而崩潰。用新代碼編輯我的答案。 –

+0

好吧,當我使用這一行cell = [[[ProductTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];它沒有崩潰,但列表完全是空的 – mikez

相關問題