在iOS中将单元格添加到UITableView的底部 [英] Add cell to bottom of UITableView in iOS

查看:94
本文介绍了在iOS中将单元格添加到UITableView的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有故事板的xcode 4.2创建一个iphone应用程序。

I am using xcode 4.2 with storyboard to create an iphone app.

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

When I press the edit button in the top right corner I would like to have the options to delete the existing rows and see the extra cell (with the green '+' icon) at the top which would allow me to add a new cell.

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

I have an array which is being populated in the viewDidLoad method using CoreData

我已启用设置按钮

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

I意识到我需要在某些时候添加单元格,然后我可以编辑但是我不清楚在哪里,我无法在互联网上找到解释。

I realise I need to add the cell at some point which I can then edit but it isn't clear to me where and I am unable to find a explanation on the internet.

推荐答案

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

Ok, the basic idea is that when the edit button is clicked we'll show the delete controls next to each row and add a new row with the add control so that users can click it in order to add an entry right? First, since you have the edit button setup already let's instruct our table that in editing mode we should show an extra row. We do that in our tableView:numberOfRowsInSection:

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

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

a_recs here is the array I've setup to store our records so you'll have to switch that out with your own array. Next up we tell our tableView:cellForRowAtIndexPath: what to do with the extra row:

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

我们还想指示我们的表,我们想要添加的那行icon:

We also want to instruct our table that for that add row we want the add icon:

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

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

Butter. Now the super secret kung fu sauce that holds it all together with chopsticks:

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

    }
}

祝你好运和好胃口!

这篇关于在iOS中将单元格添加到UITableView的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆