There are 2 possible scenarios to solve your problem:
1. If you have a static UITableView, that is, all cells configured in XIB or Storyboard, you cannot insert or delete cells, because your whole table view is configured through Interface Builder and thus cannot be changed. The way to do this is to configure all cells (including those that should be shown later) in Interface Builder, and then adjust their height at runtime:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0)
return _firstRowVisible ? 44 : 0;
return 44;
}
- (IBAction)showOrHideEmailField:(id)sender
{
_firstRowVisible = !_firstRowVisible;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];
}
2. , Interface Builder, . -tableView:cellForRowAtIndexPath: , , UITableView -dequeueReusableCellWithIdentifier:forIndexPath: .
-insertRowsAtIndexPaths:withRowAnimation:, , .
:
@property (assign, nonatomic) NSInteger numberOfEmailFields;
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return 6;
case 1:
return self.numberOfEmailFields + 1;
default:
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
{
switch (indexPath.row) {
case 0:
{
static NSString *CellIdentifier = @"MyCell";
return [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
...
default:
break;
}
break;
}
default:
break;
}
return nil;
}
- (IBAction)addEmailField:(id)sender
{
self.numberOfEmailFields++;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
, .
, , . , .