2013-10-31 91 views
0

我是新來的ios,我有以下問題。ios增加/減少表格大小

我想根據表視圖中元素的數量增加和減少tableview的高度。如果在輸入時客戶端輸出3或3個以上的元素,我想看到一個比默認值大2行的tableview。當輸出中元素的數量是2或1時,我會看到默認的表格視圖高度。寬度將是相同的。這個怎麼做?

這是我的代碼:

if (inputList.count>2) 
    { 
      CGRect bounds = [self.tableView bounds]; 
     [self.tableView setBounds:CGRectMake(bounds.origin.x, 
             bounds.origin.y, 
             bounds.size.width, 
             bounds.size.height + 50)]; 
     CGRect tableFrame = [ self.predictiveDialog frame]; 
       tableFrame.size.height=75; 
     [self.tableView setFrame:tableFrame]; 

    } 
    else 
    { 
     NSLog(@"decrease size"); 

     [self.tableView setBounds:CGRectMake(bounds.origin.x, 
                bounds.origin.y, 
                bounds.size.width, 
                bounds.size.height - 50)]; 
     CGRect tableFrame = [ self.predictiveDialog frame]; 
     tableFrame.size.height=44; 
     [self.tableView setFrame:tableFrame]; 

    } 

在這種情況下,實現代碼如下寬度爲OK,並且高度是確定當輸入元件進來,但根據無在inputlist元素 - 的tableview中被向上移動,並在ios屏幕上。爲什麼?

+0

你想增加表視圖的框架或表視圖中的單元格的高度? –

+0

我想增加tableview的高度。 –

+0

請參考我的答案並相應地設置邊界。你的代碼中的「邊界」是指什麼?我的意思是哪個視圖。我猜想它的一些其他觀點 –

回答

1

你爲什麼不以cellForRowAtIndexPath:(NSIndexPath *)indexPath委託方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if([indexPath row]>2) 
    { 

     [tableView setBounds:CGRectMake(self.tableView.frame.origin.x, 
             self.tableView.frame.origin.y, 
             self.tableView.frame.size.width, 
             self.tableView.frame.size.height + 50)]; 

    } 

    // your other code 
} 

希望這可以幫助您設置界限。希望我正確理解你的問題。

0

試試這個,

CGFloat height = self.tableView.rowHeight; 
height *= inputList.count; 

CGRect tableFrame = self.tableView.frame; 
tableFrame.size.height = height; 
self.tableView.frame = tableFrame; 
+0

感謝您的評論。我已經測試過這個,但是每次我輸入越來越多的元素時,tableview會進入ios屏幕的底部。 –

+0

因此,您可以限制表視圖將增長到的高度... – Wain

0
CGRect bounds = [self.tableView bounds]; 
[self.tableView setBounds:CGRectMake(...)]; 
CGRect tableFrame = [ self.predictiveDialog frame]; 
tableFrame.size.height=75; 
[self.tableView setFrame:tableFrame]; 

代碼[self.tableView setBounds:CGRectMake(...)]是無用的,因爲在這裏你最後設置的tableView的框架。

爲了避免您的tableView移動,您可以使用tableFrame.origin.y = self.tableView.frame.origin.y