在 UITableView 中使用插入行 [英] Using insert rows in a UITableView

查看:28
本文介绍了在 UITableView 中使用插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在使用下面的代码来执行此操作,但问题是没有像联系人那样平滑过渡.相反,新行突然出现.如何获取动画?

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?

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

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.

谢谢.

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

推荐答案

我错过了一件事.在 setEditing: 中,我不应该调用 reloadData:

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

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

这篇关于在 UITableView 中使用插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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