将单元格添加到iOS中UITableView的底部

rog*_*oom 8 iphone storyboard uitableview ios

我正在使用xcode 4.2和storyboard来创建一个iphone应用程序.

当我按下右上角的编辑按钮时,我想有删除现有行的选项,并在顶部看到额外的单元格(带有绿色的"+"图标),这样我就可以添加一个新的单元格.

我有一个viewDidLoad使用CoreData 在方法中填充的数组

我已启用设置按钮

self.navigationItem.rightBarButtonItem = self.editButtonItem;
Run Code Online (Sandbox Code Playgroud)

并实施了该方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:   
          (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
                   (NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // removing a cell from my array and db here... 
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // adding a cell to my array and db here...
    }   
}
Run Code Online (Sandbox Code Playgroud)

我知道我需要在某个时刻添加单元格然后我可以编辑但是我不清楚在哪里,我无法在互联网上找到解释.

rag*_*fin 20

好的,基本的想法是,当单击编辑按钮时,我们将显示每行旁边的删除控件,并添加一个带有添加控件的新行,以便用户可以单击它以便添加一个条目吗?首先,因为您已经设置了编辑按钮,所以让我们指示我们的表格在编辑模式下我们应该显示一个额外的行.我们在我们这样做tableView:numberOfRowsInSection:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.editing ? a_recs.count + 1 : a_recs.count;
}
Run Code Online (Sandbox Code Playgroud)

a_recs这是我设置的数组,用于存储我们的记录,因此您必须使用自己的数组切换出来.接下来我们告诉我们tableView:cellForRowAtIndexPath:如何处理额外的行:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";
    BOOL b_addCell = (indexPath.row == a_recs.count);
    if (b_addCell) // set identifier for add row
        CellIdentifier = @"AddCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if (!b_addCell) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }

    if (b_addCell)
        cell.textLabel.text = @"Add ...";
    else
        cell.textLabel.text = [a_recs objectAtIndex:indexPath.row];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我们还想指示我们的表,为那个添加行我们想要添加图标:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == a_recs.count) 
        return UITableViewCellEditingStyleInsert;
    else
        return UITableViewCellEditingStyleDelete;
}
Run Code Online (Sandbox Code Playgroud)

牛油.现在超级秘密的功夫酱与筷子一起保存:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    if(editing) {
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:a_recs.count inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];        
    } else {
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:a_recs.count inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];
        // place here anything else to do when the done button is clicked

    }
}
Run Code Online (Sandbox Code Playgroud)

祝你好运和好胃口!