2016-06-17 18 views
-1

私は4つのセルでtableViewを作成しています。そして、細胞は、装置の向きに応じて様々なサイズに分割されなければならない。動的にセルの位置をUITableViewで設定する方法

第1列目と第2列目は同じ幅を持ち、3列目はすべて最大であり、4列目は3列目より小さくなりますが、1列目と2列目以上です。

動的に行う方法はありますか?

Here is the template

私は幅を計算する際にトラブルが生じています、ここで私がこれまで持っているものです。私はそれ

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath]; 
UIInterfaceOrientation newOrientation = [UIApplication sharedApplication].statusBarOrientation; 
UILabel *typeLabel = [[UILabel alloc] init]; 
if (cell==nil) 
{ 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cells"]; 
} 

if(newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) 
{ 
    l1 = [[UILabel alloc] initWithFrame:CGRectMake((self.tableView.bounds.size.width/15+20), 50, self.tableView.bounds.size.width/12, 21)]; 

} 
else 
{ 
    l1 = [[UILabel alloc] initWithFrame:CGRectMake((self.tableView.bounds.size.width/15+50), 50, self.tableView.bounds.size.width/12, 21)]; 
} 


[cell.contentView l1]; 
[cell.contentView l2]; 
[cell.contentView l3]; 
[cell.contentView l4]; 

return cell; 
} 
+0

等幅オプションを使用する必要がありますか?あなたはあなたが心配する必要がない様々な画面サイズをサポートできるので、自動レイアウト機能を使用しようとすることができます、それは最高のソリューションです。 –

+0

自動レイアウトとは?プログラムでテーブルを作成しています – drbj

答えて

0

「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]; 

    } 
+0

ありがとうございますが、プログラムでテーブルを作成していますので、xibはありません – drbj

+0

はい、ダイナミックテーブルセルでも機能します。動的である場合、各セルの計算量が少なくなります。ただし、上記のプログラムで計算されたラベルを確認することはできます。私はコードを更新しました。 –

+0

ありがとうございます。テーブルが唯一のビューではないので、self.viewの代わりにtableViewを使用しています。今、デバイスを左右に回転させると問題が発生します – drbj