2012-04-23 77 views
0

這可能需要一點解釋。我有一個UIViewController子類,其中包含一個標籤和一個tableview正下方(標籤和一些其他控件是我不能使用UITableViewController子類的原因)。由於tableview內容是動態的,因此當我添加它時,我無法直接設置它的高度。所以,我在做的loadView如下:UIViewController中動態大小的tableview

//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]; 

這種運作良好,到目前爲止(雖然替代的想法將是很好聽)。

當我爲我的視圖進入編輯模式時出現問題。我想修改表視圖的內容,插入額外的部分和行。到目前爲止,我有以下實施setEditing的:

- (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]; 

}

的附加部分和行添加就好了,但表視圖的底部行被切斷,因爲它的規模還沒有已更新。我應該怎麼做?

我試圖再次使用相同的代碼 - layoutIfNeeded,然後手動設置高度,但是這似乎並沒有做任何事情

我很想知道我應該看動態調整進入和退出編輯時的tableview。

回答

-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派生它的工作