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

标签 iphone ios uitableview storyboard

我正在使用带有 Storyboard的 xcode 4.2 来创建一个 iphone 应用程序。

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

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

我已经启用了设置按钮

self.navigationItem.rightBarButtonItem = self.editButtonItem;

并实现了方法

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

我意识到我需要在某个点添加单元格,然后我可以对其进行编辑,但我不清楚在哪里,而且我无法在 Internet 上找到解释。

最佳答案

基本思路是,当单击编辑按钮时,我们将在每一行旁边显示删除控件,并使用添加控件添加新行,以便用户可以单击它来添加条目,对吗?首先,由于您已经设置了编辑按钮,让我们指示我们的表格在编辑模式下我们应该显示一个额外的行。我们在 tableView:numberOfRowsInSection 中这样做:

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

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

我们还想指示我们的表,对于该添加行,我们需要添加图标:

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

黄油。现在用筷子把它夹在一起的 super secret 功夫酱:

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

    }
}

祝你好运,胃口好!

关于iphone - 在 iOS 中将单元格添加到 UITableView 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9874917/

相关文章:

iphone - 使用不带 alertView 的 UIWebView 在 iOS 中以编程方式调用电话

iphone - ios 将 ipad 应用程序转换为通用显示此错误 Nib 但未设置 View socket

iphone - 无法更改 TextView 中的文本

ios - UILabel 在第一次滚动时不会更改 UITableViewCell 中的高度

ios - 在iOS 7中的cellForRowAtIndexPath之后没有调用heightForRowAtIndexPath

iphone - 错误 : "Expected specifier-qualifier-list before ' RootViewController'"in delegate header

iphone - 无法通过在 ipad 中使用 OpenGL 获得裁剪图像的确切部分

ios - 在项目中使用两个静态库时,由于两个静态库中编译的同名类而发生冲突

ios - Objective C - UITableViewCell 中的 ImageView(自定义复选标记)在选择多个单元格后停止切换

ios - 使用 Swift 在 TableView 中重新加载单元格数据