objective-c 在 UITableView 中使用插入行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1470898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:14:58  来源:igfitidea点击:

Using insert rows in a UITableView

objective-ciphoneuitableviewinsertediting

提问by Bill

I'd like my UITableView to behave like the the table in the Contacts editor, i.e. the user should hit Edit and an "add new category" row should appear at the bottom of each section.

我希望我的 UITableView 表现得像联系人编辑器中的表格,即用户应该点击编辑并且“添加新类别”行应该出现在每个部分的底部。

I'm using the below code to do this, but the problem is that there is no smooth transition as there is in Contacts. Instead, the new row suddenly appears. How can I get the animation?

我正在使用下面的代码来做到这一点,但问题是没有像 Contacts 那样平滑过渡。相反,新行突然出现。我怎样才能得到动画?

Also, how do I respond to clicks on the "add new category" row? The row is not clickable in my current implementation.

另外,我如何响应点击“添加新类别”行?在我当前的实现中,该行不可点击。

Do I need to reload the data when the user starts editing? I am doing this because otherwise the insertion rows are never drawn.

用户开始编辑时是否需要重新加载数据?我这样做是因为否则永远不会绘制插入行。

Thanks.

谢谢。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section {
    // ...
    if( self.tableView.editing ) 
        return 1 + rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // .....
    NSArray* items = ...;
    if( indexPath.row >= [items count] ) {
        cell.textLabel.text = @"add new category";
    }
    // ...

    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray* items = ...;

    if( indexPath.row == [items count] )
        return UITableViewCellEditingStyleInsert;

    return UITableViewCellEditingStyleDelete;
}

回答by Bill

I was missing one thing. In setEditing:, instead of calling reloadData I should have done:

我错过了一件事。在 setEditing: 中,我应该做的不是调用 reloadData:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController

    NSMutableArray* paths = [[NSMutableArray alloc] init];

    // fill paths of insertion rows here

    if( editing )
        [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
    else
        [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];

    [paths release];
}

回答by luvieere

Responding to clicks on the row might be done in the didSelectRowAtIndexPathmethod, for indexPath.row == [items count]. For the animation, I suggest taking a look here, at the insertRowsAtIndexPaths:withRowAnimation:method. There's a post on how to use it here.

响应行上的点击可能在didSelectRowAtIndexPath方法中完成,对于indexPath.row == [items count]. 对于动画,我建议看一下这里insertRowsAtIndexPaths:withRowAnimation:方法。有一个关于如何使用它后在这里

回答by bio

An unwanted side effect of the highlighted solution is that the "add" row is inserted also when the user just swipes one single row (provided that swiping is enabled). The following code solves this dilemma:

突出显示的解决方案的一个不需要的副作用是,当用户只滑动一行时(如果启用了滑动),也会插入“添加”行。下面的代码解决了这个难题:

// Assuming swipeMode is a BOOL property in the class extension.

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Invoked only when swiping, not when pressing the Edit button.
    self.swipeMode = YES;
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.swipeMode = NO;
}

Your code would require a small change:

您的代码需要一个小的改动:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController

    if (!self.swipeMode) {

        NSMutableArray* paths = [[NSMutableArray alloc] init];

        // fill paths of insertion rows here

        if( editing )
            [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
        else
            [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
        [paths release];
    }
}