Ios 如何将单元格添加到静态分区UITableView中?

Ios 如何将单元格添加到静态分区UITableView中?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有点困惑 - (IBAction)addEmailField:(id)sender { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; UITableViewCell *Cell = [self.tableView cellForRowAtIndexPath:indexPath]; [self.tableView beginUpdates];

我有点困惑

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

    }
这甚至不会编译,我阅读了文档,我知道我必须通过NSarray。但我不知道如何将单元格添加到该数组中

- (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
}

无需指定开始更新/结束更新,仅当表上有一系列操作(如插入、移动、删除等)时才应使用。

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

1。如果您有静态UITableView,即在XIB或情节提要中设置的所有单元格,则无法插入或删除单元格,因为您的整个表格视图是通过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];
    }
2.如果您有动态单元格,您可以在Interface Builder中设置它们的外观,为每个单元格类型设置自己的重用标识符。然后,在代码中的
-tableView:cellForRowAtIndexPath:
中,通过将相应的单元格标识符传递给UITableView的
-dequeueReusableCellWithIdentifier:forindexath:
指定要使用的单元格类型,并适当设置单元格

要使用
-insertRowsAtIndexPaths:withrowsanimation:
,您需要首先更新数据源,以保持视图与模型同步。 因此,对于您的情况,以下代码可能会起作用:

@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];
}
我使用“动态单元格”方法为您的场景创建了一个


请注意,要将单元格添加到表视图中,需要使用第二种方案,即使用动态单元格。起初,这似乎需要更多的工作,但这种方法更易于扩展,将来更易于修改。

您好,感谢您的重放,我不想隐藏单元格,而是创建它们,因为每次按下按钮时都会添加一行。我发布了一个新的我编辑了我的原始答案,包括关于如何动态创建新单元格的更详细的解释,并添加了一个示例项目,使其更容易获得。如果我的答案对你有帮助,请把它标为正确!哇,谢谢你!!正是我需要的