Search code examples
iosuitableviewcell

How do I add a cell to a UITableView?


In my storyboard, I have a UITableView. When a button is pressed (action is createGroup:), I would like to create a new cell in the table view with the text @"Cell" for now. How do I do this? I've looked over many questions, but I can't rally understand any. I need help.


Solution

  • That's normal, besides adding a new row (visually), you need to update your Data Source (the notifications array in this case). The UI is just the visualization of your Data Source. So:

    [array addObject:@"a new Entry"];
    
    [[self tableView] insertRowsAtIndexPaths:paths
                            withRowAnimation:UITableViewRowAnimationTop];
    

    And when you delete:

    [array removeObjectAtIndex:indexPath.row];
    
    [[self tableView] deleteRowsAtIndexPaths:paths
                            withRowAnimation:UITableViewRowAnimationTop];