0

When I run my ios app, only the first cell displays in the TableView, then when clicked the second cell displays. I would like them both to display at the same time, but I cannot figure out this behavior.

Here is the code for my view did load

- (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;
}

Any help would be much appreciated!

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
mikez
  • 160
  • 1
  • 15

1 Answers1

0

Since you are loading your cell from Interface builder try this:

- (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;
}

Change this:

static NSString *CellIdentifier = @"ProductCellId";

to:

static NSString *CellIdentifier = @"ProductTableCell";

You can find additional information for what you are trying to do in this question: Load cells from XIB files

Community
  • 1
  • 1
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Thanks for the response. When my (cell== nil) condition has this { [[NSBundle mainBundle] loadNibNamed:@"ProductTableCell" owner:self options:nil];cell = self.productCell; } it does the behavior i described. When I put (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } the app simply crashes. Any further suggestions? Thanks again! – mikez Jul 09 '11 at 05:56
  • @Mikez Why does the app crash, what is the error?? I am assuming ProductTableCell is a UITableView sub class so to be more specfic you would have to do [[[ProductTableCell alloc], that is probably why it is crashing, since it would crash with a ClassCastException on the dequeu. Edited my answer with the new code. – Oscar Gomez Jul 09 '11 at 05:59
  • Okay, when i used this line cell = [[[ProductTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; it didn't crash, but the list was completely empty – mikez Jul 09 '11 at 06:05
  • @Mikez Well that doesn't have to do with tableviewcell anymore, but with filling the cells correctly. I think you will find this tutorial helpful: http://www.iphonesdkarticles.com/2009/03/uitableview-drill-down-table-view.html – Oscar Gomez Jul 09 '11 at 06:10
  • Wouldn't the cells be filled correctly if they worked before (all the information was correctly placed on the first cell, and the second when the first was selected)? – mikez Jul 09 '11 at 06:12
  • What do you load here exactly : [[NSBundle mainBundle] loadNibNamed:@"ProductTableCell" owner:self options:nil]; try leaving that line as well as my code in the cell == nil – Oscar Gomez Jul 09 '11 at 06:16
  • The list is still empty afterwards – mikez Jul 09 '11 at 06:18
  • @Mikez see my edited question, that should solve the problem. – Oscar Gomez Jul 09 '11 at 06:27
  • Still have the same problem. I made the ID of the table cell ProductCellId so that was correct initially. Thank you for the link and your help, I will investigate further. – mikez Jul 09 '11 at 06:32