2012-04-23 5 views
0

これは少し説明するかもしれません。私はUIViewControllerサブクラスをラベルとテーブルビューの下に直接(ラベルと他のいくつかのコントロールは私がUITableViewControllerサブクラスを使用することができない理由である)を含んでいます。テーブルビューの内容は動的なので、追加するとその高さを直接設定できません。だから、私はloadViewで次のようにしています。UIViewControllerで動的にサイズ変更されたテーブルビュー

//add the table view with no size for now 
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped]; 
    tableView.dataSource = self; 
    tableView.delegate = self; 
    tableView.scrollEnabled = NO; 

//now that the datasource is set, use the delegates to update the height 
    [tableView layoutIfNeeded]; 
    [tableView setFrame:CGRectMake(0, self.y_position, 320, [tableView contentSize].height)]; 

//add it to my scrollview, which contains all controls in this view 
    [scrollView addSubview:tableView]; 

これはうまくいきます。

ビューの編集モードに入ると問題が発生します。追加のセクションと行を挿入して、テーブルビューの内容を変更したいと思います。それのサイズがいないので

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ 
    [tableView beginUpdates]; 
    [super setEditing:editing animated:animated]; 

    //insert or remove the section and row 
    NSRange range = NSMakeRange(0, 1); 
    if (self.isEditing) { 
     [tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:range] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
    else { 
     [tableView deleteSections:[NSIndexSet indexSetWithIndexesInRange:range] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 

    [nameAndIngredientsTableView endUpdates]; 

}

追加セクションと行がうまく追加されますが、テーブルビューの一番下の行が切断されています。これまでのところ、私はsetEditingの次の実装を持っています更新されました。私はこれをどのようにするべきですか?

私は再び同じコードを使用しようとしました - layoutIfNeededをし、手動で高さを設定するが、これは私が動的にサイズを変更するために見てしなければならないものを知ってみたいもの

を行うにはいないようです編集中に出入りするときのテーブルビュー。

答えて

-1

スクロールせずにすべての行を表示しようとしているとします。 (アプリケーションがスクロールしても問題ない場合は、最後の行を切り捨てても問題ありません)

これを管理するより良い方法は、行の高さで再生することです。フレームの高さを固定します。次に、rowHeightをframeHeight/NumberOfRowsとして計算します。

・ホープこのことができます

0

私はよりよい解決策がある場合に開く残しておきますが、私はこれだけは手動で新しい高さを計算しても、手動でアニメーションで仕事を得ることができます:

[UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    [nameAndIngredientsTableView setFrame:CGRectMake(0, tableView.frame.origin.y, 320, tableView.frame.size.height + delta)]; 
    [UIView commitAnimations]; 

デルタは挿入の場合は+ x、削除の場合は-xです。 xは現在理想的ではない試行錯誤によって決定されていますが、私はtableView.rowHeight、.sectionHeaderHeight、および.sectionFooterHeightから派生するために取り組んでいます

関連する問題