代码之家  ›  专栏  ›  技术社区  ›  Bill

在UITableView中使用插入行

  •  32
  • Bill  · 技术社区  · 17 年前

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

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

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

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

    谢谢。

    - (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;
    }
    
    3 回复  |  直到 9 年前
        1
  •  36
  •   runmad    14 年前

    我错过了一件事。在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];
    }
    
        2
  •  5
  •   pablasso Jordan    15 年前

    响应行上的点击可以在 didSelectRowAtIndexPath indexPath.row == [items count] 。对于动画,我建议看一看 here ,在 insertRowsAtIndexPaths:withRowAnimation: 方法。有一篇关于如何使用它的帖子 here .

        3
  •  0
  •   bio    10 年前

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

    你的代码需要稍作修改:

    - (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];
        }
    }
    
    推荐文章