3

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];
}
Hiren
  • 12,720
  • 7
  • 52
  • 72
ruelluna
  • 787
  • 1
  • 11
  • 30
  • What you've done is not a good way to deal with `UITableView`s! You've created `UITableViewCell`s elsewhere while it is recommended to be done in `cellForRowAtIndexPath`. You can do what you want by simply changing your `dataSource` and then reloading the table. – tipycalFlow Apr 02 '12 at 06:21

3 Answers3

1

Hi try reading this:

How to display the UITableView programmatically?

Adding cells programmatically to UITableView


Hope this could help you.

Community
  • 1
  • 1
Bazinga
  • 2,456
  • 33
  • 76
  • Thank you, but I have no problem in adding cells dynamically into a tableview. My concern is how to add a section and a cell to that newly added section. As we know, we have to configure the ,- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView method to return the number of sections. I have a counter to do this when the button is pressed then reload the table after. The problem is how can I handle the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method that controls the if statement for each section to appear? – ruelluna Apr 02 '12 at 03:37
  • 1
    Please suggest links in comments not as an answer. – rohan-patel Apr 02 '12 at 05:57
0

You only have to add the new section and the new item to the array that holds the section and the items like so:

- (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 needs:

[self.tableView reloadData];
Lima Neto
  • 53
  • 1
  • 9
0

First, update your's datamodel(NSArray or mutable). Second, when you want tableview refresh, add a code [self.tableView reloadData];


oops, your code some strange uitaleviewDataSource, Delegate Method.

also, your have a some mistake.

why you implements numberOfRowsInSection return 1? very strange.

I apprecated below code.

CASE 1. No has 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) does not necessarily need, so I simply rowCount variable added and your rowsCount of tableviewCell has synchronized.

If you wanna with DataModel, below CASE2. refer plz.


CASE2. has 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;
}
bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43
  • The problem is how can I handle the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method that controls the if statement for each section to appear? – ruelluna Apr 02 '12 at 03:44
  • Multisection is mean equal 2d array model, if you want extend section, your's datamodel must 2d array implements. Also About tableView numberOfSectionInTableview method add a code return [myArray count]; implements too. – bitmapdata.com Apr 02 '12 at 03:53
  • numberOfSection implements return [myArr count]; numberOfRowsInSection must implements return [[myArr objectAtIndex:section] count]; – bitmapdata.com Apr 02 '12 at 03:57
  • Still don't get it. I updated my question with my codes. Please see if this is clear to you. Thank you so much. – ruelluna Apr 02 '12 at 04:31