2012-10-21 14 views
11

私は可変数の行(セル)を持つUITableViewを持っています。これらの行の高さは一定です。私が望むのは、UITableViewの高さを行の数に依存させることです。セル数に基づくUITableViewの高さ

また、私はUITableViewがセルを正確に折り返しているようにしたいので、底部には余白がありません。したがって、セルの高さが60の場合、テーブルビューは1つのセルで60、2つのセルで120などです。

ありがとうございます!

答えて

28

データソースにアクセスして、表示されるセルの数を見つけることができます。この数値に1行の高さを掛けて、テーブルビュー全体の高さを求めます。テーブルビューの高さを変更するには、そのフレームプロパティを変更します。

テーブルビューの1つの行の(一定の)高さにアクセスするには、rowHeightプロパティにアクセスします。あなたがテーブルビューではなく、テーブルビュー自体を尋ねることによって含まれていますどのように多くの行も見つけることができ

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

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

:あなたが記入した場合は、配列のオブジェクトを使用して、テーブルビューがmyArrayと呼ばれ、次のコードを使用することができますデータオブジェクトのこのようなものはうまくいくはずです:

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デリゲートメソッドを実装します。セルの行は、viewDidLoadメソッドで

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

各セルに高さを割り当て、このコードを実装し、それを作成するたびに

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

注: - yourArrayがどのように多くの行したいというデータが含まれているNSArrayのです木箱に入れる。 yourArrayが動的に値を取得する場合

OR

して、値を取得した後、このメソッドを呼び出します。

-(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の高さを設定するには、デリゲートメソッドtableView:numberOfRowsInSection:を使用することができ、@の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

あたりとしての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; 
} 

self.myTableView.rowHeight = 60.0; 

あなたは、テーブルビューを開始rowHeightを定義することも忘れないでください

...しかし、あなたの目標は、単にテーブルの空のセルを非表示にする場合は、あなたがそれに空のビューを追加する必要があります代わりにtableFooterViewだと、すべてのコードを無視します(ので、この最初のを試してみてください)上:

self.myTableView.tableFooterView = [UIView new]; 
関連する問題