内容に応じてテーブルビューの行の高さを自動調整する方法や解決策はありますか?つまり、行の高さを指定せず、行の内容に応じて行の高さを指定したいのですが、そのような方法がない場合は教えてくださいデバイスの向きが変わったときに高さを変更するにはどうすればよいですか?
6 に答える
コンテンツの高さを見つけて使用できるようにする必要があります
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return "your content height + your padding height value";
}
向きを変更したら、テーブルビューをリロードするだけです。
これをチェックしてください
// --dynamic cell height according to the text--
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = <your text>;
CGSize constraint = CGSizeMake(210, 20000.0f);
CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Helvetica-Light" size:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// constratins the size of the table row according to the text
CGFloat height = MAX(size.height,60);
return height + (15);
// return the height of the particular row in the table view
}
お役に立てれば
オリエンテーションの編集この方法を試しましたか?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
または、テーブルセルがラベルやイメージビューなどでカスタム化されている場合は、それぞれでsetAutoresizingMask:を使用して、向きに自動調整できます。
[yourview setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
あなたは実装することができます
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewのデリゲートメソッド。
あなたがそれをグーグルするならば、あなたはそれについてたくさんのチュートリアルを見つけることができるはずです。
たとえば、次のようになります。
http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
あなたが使用する必要があります:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
ええ-私はあなたがこの方法をよく知っていることを知っています、そして私はあなたの質問を注意深く読みました。
- 行の高さを動的に制御するには、そのデリゲートメソッドを使用する必要があります。私の提案は、テーブルビューのバックエンドデータモデルから各セルのコンテンツを調べて、そのセルに適切な数値を次のように返すことです。
NSDictionary *thisCell = (NSDictionary *)[myArray objectAtIndexPath:indexPath.row];
NSString *myCellContents = [thisCell valueForKey:@"MyStringIWantToCheckOut"];
if ([mycellContents length] > 25) {
return 80;
} else {
return 40;
}
向きが変わる場合は、デリゲートメソッドでそれを行う必要があります。
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
その後、に発砲reloadData
しTableView
ます。
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row){
case 0:
if(indexPath.section == 0)
//title
return 75;
default:
return 75;
}
}
func tableView(_ heightForRowAttableView:UITableView、heightForRowAt indexPath:IndexPath)-> CGFloat {return UITableViewAutomaticDimension}
行の動的な高さの場合、それ以外の場合は「UITableViewAutomaticDimension」の代わりに静的な高さを指定できます