Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom `UITableView` with different cell id's. I'm trying to add a row when the button gets selected. Here is my code:

Objective-C
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.numberOfRows;
}

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

    NSString *cellID;

    ...
    else if ([indexPath row] == 5) {
        cellID = @"rowFive";
    }
    else if ([indexPath row] >= self.myNum && [indexPath row] <= self.myNum) {
        cellID = @"rowSix";
    }
    else if ([indexPath row] == self.myNum + 1) {
        cellID = @"rowSeven";
    }
    else if ([indexPath row] == self.myNum + 2) {
        cellID = @"rowEight";
    }

    customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[customCell alloc] init];
    }

    return cell;
 }

- (IBAction)addRow:(id)sender
{
    self.myNum ++;
    self.numberOfRows ++;
    [self.myTableView reloadData];
}


What happens is, when I select the button, a row gets added, but nothing inside of it. It's just an empty row, and it doesn't get loaded with the cell id of `rowSix`.

When I delete the if statement of `cell == nil`, then, when I select the button, the app crashes with an error.
Posted

1 solution

It is absolut that what you have coded. Set a breakpoint in cellForRowAtIndexPath: and you will see it. If you create a new cell all values are as initialized (or empty). You must fill it with some values.

Have you created that cell type in the interface builder in the used tableview.

PS: your coding style is messy. A cleaner style helps avoiding bugs and building good apps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900