Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Section and TableView Cell dynamically

I have a button that when pressed, a tableview section will be added and will also add a new row to that section. How can I implement this programatically?

I have an array of cells.

Here are my codes

- (IBAction)addCell:(id)sender {

    UITableViewCell *newCell = [[UITableViewCell alloc] init];

    counter++;
    [cells addObject:newCell];

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [cells count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      //this is where i want to adjust the row per section
      //this code hide the recent sections, only the current created section appears
      return [cells objectAtIndex:indexPath.row];
}
like image 448
ruelluna Avatar asked Mar 05 '26 07:03

ruelluna


1 Answers

Hi try reading this:

How to display the UITableView programmatically?

Adding cells programmatically to UITableView


Hope this could help you.

like image 85
Bazinga Avatar answered Mar 07 '26 20:03

Bazinga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!