【问题标题】:Add cell to bottom of UITableView in iOS在 iOS 中将单元格添加到 UITableView 的底部
【发布时间】:2012-04-10 02:54:45
【问题描述】:

我正在使用带有故事板的 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...
    }   
}

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

【问题讨论】:

    标签: iphone ios uitableview storyboard


    【解决方案1】:

    好的,基本思想是,当单击编辑按钮时,我们将在每一行旁边显示删除控件,并使用添加控件添加一个新行,以便用户可以单击它来添加条目,对吧?首先,由于您已经设置了编辑按钮,让我们指示我们的表格在编辑模式下应该显示一个额外的行。我们在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;
    }
    

    黄油。现在是用筷子夹在一起的超级秘方功夫酱:

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

    祝你好运,祝你好运!

    【讨论】:

    • 使用此方法时,我注意到我们正在添加和删除的插入行在删除时会在右侧短暂显示蓝色选择图标。它与在删除行之前使表格退出编辑模式有关。我尝试将beginUpdates 调用移到tableView setEditing 调用上方,但是当添加行而不是删除行时会出现蓝色图标。任何想法如何避免那个蓝色的选择图标?
    • 听起来accessoryType 被设置在某处或缓存在tableView 队列中。添加单元格时尝试将其显式设置为 UITableViewCellAccessoryNone。
    【解决方案2】:

    This tutorial 值得一读,应该对您有所帮助。它展示了如何在 UITableView 的底部设置“添加新行”UITableView 行,但您应该能够在您的实现中将其显示在 UITableView 的顶部:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    例如

    if(self.editing && indexPath.row == 1)
    {
        cell.text = @"Insert new row";
        ...
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多