WWDC 11のビデオ 'セッション125 - UITableCiew Changes、Tips、Tricks' at 24:30+ Mr Luke Hiestermanは、セルが選択されたときにテーブルビューにセルを追加するデモを行っています。UITableViewのアクション行
私は私の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行目に呼び出されました(新しいカスタムである必要があります)
どうすれば新しいものかチェックできますか?
挿入/削除の開始時と終了時に[tableView beginUpdates]と[tableView endUpdates]を呼び出す必要があります。 –