How to add a cell to a static section of a UITableView?

I'm a little confused

   - (IBAction)addEmailField:(id)sender {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell *Cell = [self.tableView cellForRowAtIndexPath:indexPath];

        [self.tableView beginUpdates];
        [self.tableView  insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView  endUpdates];

    }

it doesn't even compile, I read the documentation I know, I have to pass NSarray to it. but i don't understand how to add a cell to this array

+3
source share
2 answers

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:

    // This will show or hide the first row in first section depending on the value of
    // BOOL _firstRowVisible instance variable that you set elsewhere in your code.
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0 && indexPath.row == 0)
            return _firstRowVisible ? 44 : 0;
        return 44;
    }

    // To show or hide the first row in first section.
    // Note that you need to call both -reloadRowsAtIndexPaths:withRowAnimation:
    // and -reloadData to make this work.
    - (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:
                {
                    // Dequeue, set up and return a cell with corresponding reuse identifier
                    static NSString *CellIdentifier = @"MyCell";
                    return [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
                }
                ...
                default:
                    break;
            }
            break;
        }
        default:
            break;
    }
    return nil;
}

- (IBAction)addEmailField:(id)sender
{
    // Update data source
    self.numberOfEmailFields++;

    // Animate rows
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}

, .

, , . , .

+9
- (IBAction)addEmailField:(id)sender {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; // lowercase ivar name

    [self.tableView insertRowsAtIndexPaths:@[indexPath]    
                          withRowAnimation:UITableViewRowAnimationRight]; // use @[] for array literals
}

/ , , , , , ..

0

All Articles