Search code examples
iosuitableviewselectioncell

Second Cell appearing only after first cell is selected in Tableview ios


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!


Solution

  • 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