How to add Section and TableView section dynamically

I have a button that when clicked will add a tableview section and also add a new row to that section. How can I implement this programmatically?

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

      

+3


source to share


3 answers


Hi read this:

How do I programmatically display a UITableView?

Programmatically adding cells to a UITableView




Hope this helps you.

+1


source


First update your datamodel (NSArray or mutable). Second, if you want to update the table, add the code [self.tableView reloadData];


oops, your code is some weird uitaleviewDataSource, delegate method.

too, you have some error.

why are you implementing numberOfRowsInSection return 1? very strange.

I figured out below code.

CASE 1. No DataModel.



- (IBAction)addCell:(id)sender 
{

    cellRowsCount++;

    [self.tableView reloadData];
}

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

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
       reuseIdentifier:CellIdentifier];
      }

     return cell;
   }

      

your codes, dataModel (NSArray or mutable) are not necessary, so I just added the rowCount variable and synchronized your rowsCount tableviewCell.

If you want with DataModel, below is CASE2. refer to plz.


Question 2. has a DataModel.

- (IBAction)addCell:(id)sender 
{

    textCount ++;

    NSString *cellText = [NSString stringWithFormat:"blah %d", textCount];
    [myArray addObject:cellText];

    [self.tableView reloadData];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [[myArray objectAtIndex:section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  if (cell == nil) 
  {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:CellIdentifier];
  }

  cell.textLabel.text = (NSString *)[myArray objectAtIndex:indexPath.row];

  return cell;
}

      

0


source


You only need to add a new section and a new element to the array containing the section and elements like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    animals = @{@"Section 1" : @[@"Item 1", @"Item 2", @"Item 3"],
                @"Section 2" : @[@"Item 1", @"Item 2"]};
}

      

after that you just reload the tableView wherever your logic is required:

[self.tableView reloadData];

      

0


source







All Articles