「CustomCell」という名前のカスタムテーブルビューのセルを作成の幅を計算する際に苦労しています原因私はまだL2、L3およびL4を作成していません。新しいFile with xibオプションを使用して作成できます。
CustomCell.h
@interface CustomCell : UITableViewCell
@end
CustomCell.m
@implementation MessagesTableViewCell
//perform your custom implementation here.
@end
CustomCell.xib 自動レイアウトを使用して、ユーザインターフェース内のセルのラベルを調整します。一定の幅を持つ最初の2つのラベルと、必要な幅の最後の2つのラベルで、4つのラベルを必要に応じて追加し、2番目のラベルと4番目のラベルに中央の(3番目の)ラベルを引き伸ばすことができます。これは、UIをはるかに簡単に管理するのに役立ちます。自動レイアウトは、さまざまなサイズのコンテンツを作成するのに役立つ機能です。あなたのテーブルビュークラス
static NSString *cellIdentifier = @"CustomCell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
私はこれがあなたの役に立てば幸いでそれを使用してhttps://www.sitepoint.com/self-sizing-cells-uitableview-auto-layout/
で自動レイアウトを使用して、動的テーブルビューセルを見つけることができます。プログラム的な計算や向きの変更
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/////////////////////////Programatic Implementation///////////////////////////////////
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
//orientation is Portrait
long widthForL1L2 = tableView.frame.size.width * .10;
long widthForL3 = tableView.frame.size.width * .60;
long widthForL4 = tableView.frame.size.width * .20;
UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, widthForL1L2, cell.frame.size.height)];
l1.backgroundColor = [UIColor redColor];
[cell addSubview:l1];
UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(widthForL1L2, 0, widthForL1L2, cell.frame.size.height)];
l2.backgroundColor = [UIColor yellowColor];
[cell addSubview:l2];
UILabel *l3 = [[UILabel alloc] initWithFrame:CGRectMake(l2.frame.origin.x+widthForL1L2, 0, widthForL3, cell.frame.size.height)];
l3.backgroundColor = [UIColor greenColor];
[cell addSubview:l3];
UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(l3.frame.origin.x+widthForL3, 0, widthForL4,cell.frame.size.height)];
l4.backgroundColor = [UIColor blueColor];
[cell addSubview:l4];
}else{
//orientation is Landscape
//perform the same calculation as above or modify it according to your needs in landscape orientation.
}
return cell;
}
と
更新されたコードの実装では、姿勢の変化のこの機能が含まれるようにを忘れないでください。この関数は、電話の向きが変更されたときにテーブルのリロードを呼び出します。
//Available iOS version >=8.0
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
//Reload the tableview on orientation change, to match the new width of the table.
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.tableView reloadData];
} completion:nil];
}
等幅オプションを使用する必要がありますか?あなたはあなたが心配する必要がない様々な画面サイズをサポートできるので、自動レイアウト機能を使用しようとすることができます、それは最高のソリューションです。 –
自動レイアウトとは?プログラムでテーブルを作成しています – drbj