2011-05-20 77 views
0

我已经挖掘并找到了几个动态添加行到UITableView的例子,而不是分组表,我只是无法弄清楚这一点。我知道我想要做什么 - 我有一个分为三部分的表格。我想在选择编辑按钮时动态添加'添加新项目'行/单元格到第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]; } 

}

回答

1

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(self.editing && section != 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 
]; 
+0

感谢您的帮助。这让我朝着正确的方向前进。现在我只需要动画...请参阅上面的编辑。 – Selch 2011-05-22 18:39:49

+0

我编辑了我的答案,并添加了如何检索索引路径。 – murat 2011-05-25 11:11:34