2011-10-18 7 views
0

WWDC 11のビデオ 'セッション125 - UITableCiew Changes、Tips、Tricks' at 24:30+ Mr Luke Hiestermanは、セルが選択されたときにテーブルビューにセルを追加するデモを行っています。UITableViewのアクション行

Screenshot from the video

私は私のIOSアプリケーションにその機能を追加したいが、私はそれを作る方法を見つけ出すことはできません。

一部のコードはデモビデオには表示されません。ダウンロード可能なデモのソースはありません。

誰でもお手伝いできますか?

EDIT:

私が選択した行の下に新しい行を追加することができますが、それは他のカスタムセルです。

(私は、あなたが受け入れることができる契約のリストを持っている)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return ((indexPath.row >= [_contracts count]) ? 40 : 60); 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [_contracts count] + ((_addedRow) ? 1 : 0); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *reuseID = @"contract_cell"; 

    ContractTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID]; 

    if(!cell) 
    { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ContractTableViewCell" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     cell = [topLevelObjects objectAtIndex:0]; 
     [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"contract_cell.png"]]]; 
    } 

    NSLog(@"row?: %d", indexPath.row); 
    //I thought this would work... not. 
    if((indexPath.row >= [_contracts count])) 
    { 
     [cell.label setText:@"new"]; 
    } 
    else 
    { 
     Contract *contract = [_contracts objectAtIndex:indexPath.row]; 

     [cell.label setText:@"test"]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(!_addedRow) 
    { 
     _addedRow = YES; 

     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

     [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
} 

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; 
} 

たとえば、私は最初の行を押してください。最初の行の下に行が追加されます。しかし今、cellForRowAtIndexPath ..が2行目に呼び出されました(新しいカスタムである必要があります)

どうすれば新しいものかチェックできますか?

+0

挿入/削除の開始時と終了時に[tableView beginUpdates]と[tableView endUpdates]を呼び出す必要があります。 –

答えて

2

追加された行を正しいインデックスに表示するには、 。現在、追加された行は、索引が契約数より多い場合にのみtableView:cellForRowAtIndexPath:に構成されているため、最後の行が最後の行であると常に仮定しています。

5つの契約があり、3つ目の契約が選択されているとします。 if((indexPath.row >= [_contracts count]))は最後の行にのみ当てはまりますが、実際にはこの条件を4番目の行でtrueにしたいので、if (indexPath.row == selectedRowIndex + 1)(選択した行インデックスをインスタンス変数に格納する必要があります)。

+0

私は半分働いています。問題は、同じセルで(追加されたセルを削除する)チェックされ、他のセルで押された場合は、前に追加されたセルを削除しなければならず、現在の新しいセルを追加する必要があります。 – Justin

0

この回答はあなたを助けるはずです。それはあなたのUITableViewControllerを「更新モード」にする方法を教えてくれますし、アニメーションを含めて新しい行を挿入することもできます:UITextField in UITableViewCell - adding new cells

+0

本当に私が望むものではありません。 (私の更新を参照してください) – Justin

関連する問題