2011-03-08 8 views
0

iPadアプリ用のテーブルビューコントローラを作成しています。このビューは、フルスクリーンまたはモーダルビュー内に表示される可能性があるため、表示されるたびにサイズが異なる可能性があります。私は理想的には、それが表示されているサイズに関係なく動作するように十分に私のコードを一般的にしたいと思います。 (学問的な練習として、私もiPhone上で動作するコードが欲しいですが、ここではそれは必須条件ではありません)定数を使用せずにグループ化されたUITableViewCellコンテンツ領域の幅を取得する方法

私はグループ化されたテーブルビュースタイルを使用しています。私はUITextFieldをセルのビューに埋め込みたいと思います。私はセル内にテキストフィールドを取得することができます(cell.AddSubviewを使用して)、しかし、私はグループ化されたスタイルを使用すると、テキストフィールドは、テーブルの左にある - 白い領域にする必要があります。

この問題の解決策のすべてに、境界領域の定数xオフセットがハードコーディングされているように見えます(例:UICatalogサンプルとその回答)。このxオフセットはiPadでは約35pxですが、iPhoneでは約20pxです。

これはもっと良い方法があるはずですが、まだ見つけていないようです。私は四角形のcell.Bounds、cell.Frame、cell.ContentView.Bounds、cell.ContentView.Frameを見てみました - グループ化されたセルの '実際の'コンテンツ領域はありません。

誰かに別の提案がありますか、値をハードコードする必要がありますか?

ありがとうございました

+0

セルを直接表示せずに、テキストフィールドをセルの「contentView」に追加します。それが間違っているなら、私たちはいくつかのコードを見る必要があります。ペン先にテキストフィールドを設定するとどうなりますか? – rgeorge

答えて

1

私も同様の回答を探しています。

「良い」答えはまだ見つかりませんが、この会話は、contentViewのフレームの呼び出しが実際のフレームからアクセサリの専用領域と丸められたエッジのパディング:

UITableViewCell's contentView's width with a given accessory type

私はバレットアドバイスを撮影しただけのハードコードされた値とに応じてフレームを調整していますが、優れている答えを見つける場合に非常に興味があると思います。

6

cell.contentViewに任意のUIViewsを追加し、このビューここ

ためautoResizeMaskプロパティを設定するには、UILabelUITextFieldcellを作成するためのサンプルです:

// custom cell implementation 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 

    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
    { 
     self.label = [[[UILabel alloc] init] autorelease]; 
     self.label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17]; 
     self.label.backgroundColor = [UIColor clearColor]; 
     [self.contentView addSubview:self.label]; 

     self.textField = [[[UITextField alloc] init] autorelease]; 

     //this will make our textField resized properly 
     self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     self.textField.borderStyle = UITextBorderStyleNone; 
     self.textField.backgroundColor = [UIColor clearColor]; 
     self.textField.textColor = [UIColor darkGrayColor]; //text color 
     self.textField.font = [UIFont systemFontOfSize:17.0]; //font size 
     self.textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 

     self.textField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard 
     self.textField.returnKeyType = UIReturnKeyNext; // type of the return key 

     self.textField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right 

     [self.contentView addSubview:self.textField]; 
     self.selectionStyle = UITableViewCellSelectionStyleNone; 
     self.accessoryType = UITableViewCellAccessoryNone; 
    } 
    return self; 
} 

とのdataSource方法で

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

そのようなもの

  CustomCell* c = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (c == nil) 
     { 
      c = [[[CellWithPass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease]; 
      c.textField.delegate = self; 
     } 
     //in this method cell and cell subviews are not resized, so 
     //we are setting subviews frames here for cell with frame {{0,0},{320,44}} 
     c.label.text = @"label"; 
     float w = [c.label.text sizeWithFont:c.label.font].width; 
     c.label.frame = CGRectMake(10, 0, w, 44); 
     c.textField.frame = CGRectMake(w + 10, 12, c.contentView.frame.size.width - w - 20, 20); 
     return c; 
関連する問題