如何将Cell添加到静态部分UITableView?

3 objective-c uitableview ios

我有点困惑

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

    }
Run Code Online (Sandbox Code Playgroud)

这甚至不会编译,我阅读文档,我知道我必须通过它NSARray.但我不明白如何将单元格添加到该数组中

max*_*lov 9

有两种可能的方案可以解决您的问题:

1.如果您有静态UITableView,即在XIB或Storyboard中设置的所有单元格,则无法插入或删除单元格,因为整个表格视图是通过Interface Builder配置的,因此无法修改.解决这个问题的方法是在Interface Builder中配置所有单元格(包括稍后需要显示的单元格),然后在运行时设置它们的高度:

    // 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];
    }
Run Code Online (Sandbox Code Playgroud)

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];
}
Run Code Online (Sandbox Code Playgroud)

我使用动态单元格方法为您的场景创建了一个示例项目.

请注意,为了能够将单元格添加到表视图,您需要使用第二种方案,即使用动态单元格.这一开始可能看起来更多,但这种方法在未来更容易扩展,更容易修改.