0

拡張可能なセルを持つ UItable があります。ユーザーがセクションをタップすると、セクションが展開されて行が表示されます。ただし、新しいセクションを開く前に、以前に開いたセクションを閉じる必要があります。私はdidselectrowでこれを行う必要があると思いますが、それを行う方法がわかりません??

didselectrow の私のコードは以下のとおりです

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{
    if(tableView.tag == 4)
    {
        //NSLog(@"Did Select Row");

        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            if (!indexPath.row)
            {
                // only first row toggles exapand/collapse
                [tableView deselectRowAtIndexPath:indexPath animated:YES];

                NSInteger section = indexPath.section;
                BOOL currentlyExpanded = [expandedSections3 containsIndex:section];
                NSInteger rows;


                NSMutableArray *tmpArray = [NSMutableArray array];

                if (currentlyExpanded)
                {
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                    [expandedSections3 removeIndex:section];

                }
                else
                {
                    [expandedSections3 addIndex:section];
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                }


                for (int i=1; i<rows; i++)
                {
                    NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                               inSection:section];
                     [tmpArray addObject:tmpIndexPath];
                }

                //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

                if (currentlyExpanded)
                {
                    [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];

                }
                else
                {
                    [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                }
            }

            else {

            }
        }

        //NSLog(@"Button Pressed?");
    }
}
4

1 に答える 1

0

reloadSections:withRowAnimation:内からUITableViewを呼び出してみてくださいtableView:didSelectRowAtIndexPath:。これにより、テーブルビューで、指定したセクションのデリゲートを再クエリできます。利点として、この方法で追加および削除されている行の優れたアニメーションが得られます。

もちろん、これは、テーブルビューの行を直接操作している現在のソリューションとはまったく異なります。したがって、試してみたい場合reloadSections:withRowAnimation:は、デリゲートのかなりの部分を書き直す必要があるかもしれません。変更には、セクションの折りたたみ/展開状態をデリゲートに直接保存するか、デリゲートによって参照されるモデルオブジェクトに保存することが含まれます。これにより、デリゲートは、テーブルビューによって再クエリされたときに、更新された戻り値を取得します。

于 2012-11-20T20:34:27.343 に答える