2012-10-21 70 views
11

我有一個可變數量的行(單元)的UITableView - 這些行的高度是不變的。我想要的是使UITableView的高度取決於行數。基於單元數的UITableView高度

此外,我想UITableView正在包裝單元格,所以沒有填充底部。所以,如果一個單元格的高度爲60,tableview應該是60個單元格,120個單元格的單元格,等等......

在此先感謝!

回答

28

您可以訪問數據源以查找將顯示的單元格數量。將此數字乘以一行的高度,即可得到整個表格視圖的高度。要更改表格視圖的高度,可以更改其框架屬性。

通過訪問rowHeight屬性,可以訪問表格視圖中一行的(常量)高度。如果你有一個數組,稱爲myArray物體填滿你的表視圖,您可以使用下面的代碼:

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

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

你也可以看看有多少行的表視圖將通過詢問表視圖本身含有,而不是的數據對象。這樣的事情應該工作:

NSInteger numberOfCells = 0; 

//finding the number of cells in your table view by looping through its sections 
for (NSInteger section = 0; section < [self numberOfSectionsInTableView:self.tableView]; section++) 
    numberOfCells += [self tableView:self.tableView numberOfRowsInSection:section]; 

CGFloat height = numberOfCells * self.tableView.rowHeight; 

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

//then the tableView must be redrawn 
[self.tableView setNeedsDisplay]; 
+0

感謝您抽出寶貴時間解決這個問題。我在我的viewDidLoad方法中注入了你的(適用於我的情況)代碼,但它不調整UITableView的大小。 –

+0

我爲你添加了另一個例子,希望它有幫助。 –

+4

好吧,找到細胞的數量不是問題,計算高度也不是問題。這是事實tableView框架的高度並沒有改變(這可能是因爲我的代碼生活在ViewDidLoad? –

2

這是我如何解決。

CGFloat height = self.mMyTicketTable.rowHeight; 

float h = height * [self.mArrEName count]; 
if(h > 410.0) 
{ 
    self.mMyTicketTable.frame = CGRectMake(0, 50, self.mMyTicketTable.frame.size.width, 410); 
} 
else 
{ 
    self.mMyTicketTable.frame = CGRectMake(0, 50, self.mMyTicketTable.frame.size.width, h); 
} 
NSLog(@"h:%f", h); 
2

實現這個UITableView委託方法。每當一個單元行創建指定高度,以每個單元

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 60.0f; 
} 

和執行viewDidLoad方法這段代碼

[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))]; 

注: - yourArray是一個NSArray其中包含了多少行,你想要的數據打包。

OR

如果yourArray取值動態再調用取值後此方法。

-(void)setHeightOfTableView 
{ 

    /**** set frame size of tableview according to number of cells ****/ 
    float height=60.0f*[yourArray count]; 

[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))]; 
} 

我希望它的作品。由於

2

我做了一個函數來設置UITableView的高度,每@ timvermeulen的代碼

-(void)setTableViewheightOfTable :(UITableView *)tableView ByArrayName:(NSArray *)array 
{  
    CGFloat height = tableView.rowHeight; 
    height *= array.count; 

    CGRect tableFrame = tableView.frame; 
    tableFrame.size.height = height; 
    tableView.frame = tableFrame; 
} 
0

如果你喜歡,你可以使用委託方法tableView:numberOfRowsInSection:動態設置UITableView的高度,這樣的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    CGFloat numberOfRows = self.myArray.count; 

    CGRect tableViewFrame = tableView.frame; 
    tableViewFrame.size.height = numberOfRows * tableView.rowHeight; 
    tableView.frame = tableViewFrame; 

    return numberOfRows; 
} 

還記得定義一個啓動表視圖0​​:

self.myTableView.rowHeight = 60.0; 

...但如果你的目標只是隱藏在表格空白單元格,你應該給它的tableFooterView而不是添加一個空的觀點,而忽略上面所有的代碼(所以先試試這個):

self.myTableView.tableFooterView = [UIView new];