如何在静态区域的UITableView中添加一个单元格?

3

我有些困惑。

   - (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。但是我不知道如何将单元格添加到该数组中。

2个回答

9

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

1. 如果您有一个静态的UITableView,即所有单元格都在XIB或Storyboard中设置,则无法插入或删除单元格,因为整个表视图是通过接口生成器进行配置的,因此无法修改。

解决方法是在接口生成器中配置所有单元格(包括稍后需要显示的单元格),然后在运行时设置它们的高度:

    // 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];
}

我使用动态单元格的方法为您创建了一个示例项目,下载链接在此处
请注意,要向表视图添加单元格,您需要使用第二种情况,也就是使用动态单元格。这乍一看似乎更加繁琐,但这种方法在未来扩展和修改时更加方便。

你好,感谢回复。我并不想隐藏单元格,而是希望每次按下按钮时都添加一行。我发布了一个新的问题,附有更多细节 - user3205189
我编辑了原始答案,包括如何动态创建新单元格的更详细说明,并添加了一个示例项目,以使其更容易理解。如果有帮助,请将我的答案标记为正确! - maxkonovalov
哇,谢谢!正是我需要的。 - user3205189

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

不需要指定的开始更新/结束更新,只有在表格上有一系列操作(例如插入、移动、删除等)时才应使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接