ios - 动态地将行添加到分组表中的多个部分

标签 ios uitableview

我已经深入研究并发现了几个将行动态添加到 UITableView 的示例,而不是添加到分组表中,但我就是无法弄清楚这一点。我知道我想做什么 - 我有一个包含 3 个部分的分组表。我想在选择编辑按钮时动态添加“添加新项目”行/单元格到第 1 和 2 部分,但不是 0。

首先我对 numberOfRowsInSection 感到困惑。我最初用这个加载我的表格。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[[tableData objectAtIndex:section] objectForKey:@"Rows"] count];

}

我是否需要在编辑时添加 if 语句,以便在编辑时向这些部分的计数中添加一行?如:

if (editing) {

    return [[[tableData objectAtIndex:section] objectForKey:@"Rows"] count] + 1;
}

我意识到,如果上述内容正确,则会向每个部分添加一行,而不仅仅是 1 和 2。我将如何限制这一点?

接下来是我的 setEditing 函数。这就是我真正的问题所在。我相信我需要创建第 1 节和第 2 节最后一行的索引路径数组,以便我可以在它们下面插入新行。下面是错误的,只是我尝试在某处插入一些东西的实验。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
NSMutableArray* paths = [[NSMutableArray alloc] initWithObjects:indexPath, nil];

NSArray *paths = [NSArray arrayWithObject:
                  [NSIndexPath indexPathForRow:6 inSection:1]];
if (editing) {
    [[self tableView] insertRowsAtIndexPaths:paths 
                            withRowAnimation:UITableViewRowAnimationTop]; }
else {
    [[self tableView] deleteRowsAtIndexPaths:paths 
                            withRowAnimation:UITableViewRowAnimationTop]; }

}

那个蹩脚的代码返回这个错误:

Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).

所以我有点迷失了。 IndexPaths 和 Arrays 对我来说是新的。实际上,这对我来说是全新的,我确实在这里翻阅了文档和帖子,但我不能说我总是理解它。任何帮助,将不胜感激。我也确实意识到我仍然需要配置我的单元格和 commitEditingStyle 方法,但我想我可以弄清楚我是否可以理解索引路径和数组。

谢谢

-----编辑-----

好的,我大部分都明白了。我在编辑时向我的部分添加了一个新行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {

if(self.editing && section != 0) {
    int rows = 0;
    rows = [[[tableData objectAtIndex:section] objectForKey:@"Rows"] count] + 1;
    return rows;
}
else {
    int rows = 0;
    rows = [[[tableData objectAtIndex:section] objectForKey:@"Rows"] count];
    return rows;
    }
}

我已经弄清楚如何在编辑模式下将标签应用到这些新行:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
static NSString *CellIdentifier= @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//configure the cell...
if(self.editing && indexPath.section != 0 && (indexPath.row == [[[tableData objectAtIndex:indexPath.section] objectForKey:@"Rows"] count])) {
    cell.textLabel.text = @"Add new Item";
    return cell;
}
else    
    cell.textLabel.text = [[[tableData objectAtIndex:indexPath.section] objectForKey:@"Rows"] objectAtIndex:indexPath.row];
return cell;

我正在正确设置两个部分的最后一行,其中该行已添加到样式插入中:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 0)
    //Can't edit the items in the first section
    return UITableViewCellEditingStyleNone;
else
    //if it's the last row in sections 1 or 2 make it an Insert item
    if(self.editing && indexPath.section != 0 && (indexPath.row == [[[tableData objectAtIndex:indexPath.section] objectForKey:@"Rows"] count])) {
        return UITableViewCellEditingStyleInsert;
    }
    //Everything else in sections 1 or 2 should be Delete style
    else
        return UITableViewCellEditingStyleDelete;

}

这一切都有效。当然它不会动画,我仍然需要帮助。我想,但我不确定是否需要从 setEditing 方法插入/删除行。我认为我需要做的是为我正在编辑的部分创建一个数组,然后在其中插入行。下面的 *paths 数组是错误的,因为我需要一个指向第 1 节和第 2 节中最后一行的数组,并且我不知道如何创建该数组。或者也许我所想的一切都是错误的。有人可以帮忙指路吗?非常感谢。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];

NSArray *paths = [NSArray arrayWithObject:
                  [NSIndexPath indexPathForRow:?? inSection:??]];
if (editing) {
    [[self tableView] insertRowsAtIndexPaths:paths 
                            withRowAnimation:UITableViewRowAnimationTop]; }
else {
    [[self tableView] deleteRowsAtIndexPaths:paths 
                            withRowAnimation:UITableViewRowAnimationTop]; }

}

最佳答案

Do I need to add an if statement for when I'm editing to add a row to the count for those sections when I'm editing?

是的,在您的 tableView:numberOfRowsInSection: 方法中,您应该在编辑模式下返回 count + 1。如果您不想将行添加到第 0 部分,则调整您的 if 条件,即 if( self.editing && 部分!= 0)

您的第二个问题与第一个问题相关。您正在向第 1 节插入新行,但是 该部分的 numberOfRowsInSection 仍返回 5。它应该返回 6。

方法 insertRowsAtIndexPaths:withRowAnimation:deleteRowsAtIndexPaths:withRowAnimation: 只是为了有效地重新加载表格并带有动画。我建议您暂时实现如下setEditing:animated 功能。然后,您应该通过考虑编辑状态来实现 tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath: 方法。一切正常后,您可以使用插入和删除方法来更好地控制重新加载表的哪些部分。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
     [super setEditing:editing animated:animated];
     [self.tableView reloadData];
}

--编辑--

您走在正确的道路上。您可以使用类似于下面的代码来获取要删除/插入的indexPaths:

NSArray *paths = [NSArray arrayWithObjects:
  [NSIndexPath indexPathForRow:[[[tableData objectAtIndex:1] objectForKey:@"Rows"] count] inSection:1],
  [NSIndexPath indexPathForRow:[[[tableData objectAtIndex:2] objectForKey:@"Rows"] count] inSection:2],
  nil
];

关于ios - 动态地将行添加到分组表中的多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067097/

相关文章:

iOS - 计算滑动次数

ios - 如何创建 STPCard Stripe ios?

ios - 更新证书和配置时的问题

iOS - TableView - 在点击按钮时获取标题部分的索引

ios - 如何快速在 UITableViewCell 中创建一个 facebook 注销按钮

ios - Alamofire 清除所有 cookie

iOS CBCentralManager 在进入范围时不会在后台检测到 CBPeripheral

ios - 控制 uitableview insertRowsAtIndexPath 动画的速度

ios - 在 UITableView 中将 x 位置设置为 UITablecell

ios - 为什么我不能将颜色数组中的颜色随机设置到 tableViewCells?