32

UITableView を連絡先エディターのテーブルのように動作させたいと思います。つまり、ユーザーが [編集] をクリックすると、各セクションの下部に [新しいカテゴリを追加] 行が表示されます。

これを行うために以下のコードを使用していますが、連絡先のようにスムーズな移行がないことが問題です。代わりに、新しい行が突然表示されます。どうすればアニメーションを入手できますか?

また、「新しいカテゴリを追加」行のクリックにどのように応答すればよいですか? 現在の実装では、行はクリックできません。

ユーザーが編集を開始したときに、データをリロードする必要がありますか? そうしないと挿入行が描画されないため、これを行っています。

ありがとう。

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

3 に答える 3

36

一つ欠けていました。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];
}
于 2009-09-25T00:30:05.730 に答える
5

行のクリックへの応答は、 のdidSelectRowAtIndexPathメソッドで行われる場合がありますindexPath.row == [items count]アニメーションについては、こちらinsertRowsAtIndexPaths:withRowAnimation:メソッドをご覧になることをお勧めします。使い方の記事はこちら

于 2009-09-24T20:50:33.987 に答える
0

強調表示されたソリューションの望ましくない副作用は、ユーザーが 1 つの行をスワイプしただけでも "追加" 行が挿入されることです (スワイプが有効になっている場合)。次のコードは、このジレンマを解決します。

// 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];
    }
}
于 2016-02-23T15:14:42.147 に答える