0

そのため、プログラムで のframeと の設定を設定UITableViewUIViewController、カスタムを使用しUITableViewCellてデータを入力しています。問題は、カスタム セルを で設定しても、幅が320で44のアンドが常にあることです。framecontentView heightinitWithFrame

私のコード:

テーブルの作成:

UITableView *pickPlayerNameTable = 
  [[UITableView alloc] initWithFrame:CGRectMake(10, 
                                                15,  
                                                (viewSize.width - 20), 
                                                4 * (viewSize.height/5) - 15)];

pickPlayerNameTable.backgroundColor = [UIColor clearColor];
pickPlayerNameTable.separatorStyle = UITableViewCellSeparatorStyleNone;
// playerNameTable.

[self.view addSubview:pickPlayerNameTable];
[self.view bringSubviewToFront:pickPlayerNameTable];

//set the controller as the tables delegate and data source
pickPlayerNameTable.delegate = self;
pickPlayerNameTable.dataSource = self;

cellRowForIndexpath メソッド:

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

NSString *cellIdentifier = @"playerNameSellectionCell";

PlayerNameCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if(cell==nil)
{
    cell = [[PlayerNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

//set the label to the corresponding player
cell.playerNumberLabel.text = [NSString stringWithFormat:@"Παίκτης %ld:", (indexPath.row + 1)];

return cell;
}

最後に、カスタムセル、initWithStlyeメソッド:

//override initWithStyle for custom cell
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

//if initialized successfully, create ui
if(self)
{
    //get the cell's frame size
    CGSize cellSize = self.contentView.frame.size;
    //CGSize viewSize = self.frame.size;
    self.backgroundColor = [UIColor blueColor];

    //set up the player number label
    self.playerNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(8.0, 4.0, (cellSize.width/2 - 16), (cellSize.height - 8))];
    [self.playerNumberLabel setFont:[UIFont fontWithName:@"Aka-AcidGR-ScrachThis" size:30]];
    [self.playerNumberLabel setTextColor:[UIColor whiteColor]];
    [self.playerNumberLabel setTextAlignment:NSTextAlignmentLeft];
    [self.playerNumberLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    //add label to cell
    [self.contentView addSubview:self.playerNumberLabel];

    //set up the textfield for the cell
    self.playerNameTextField = [[UITextField alloc] initWithFrame:CGRectMake( cellSize.width/2, 4.0, (cellSize.width/2 - 8), (cellSize.height - 8))];
    [self.playerNameTextField setBackgroundColor:[UIColor whiteColor]];
    [self.playerNameTextField setBorderStyle:UITextBorderStyleRoundedRect];

    [self.contentView addSubview:self.playerNameTextField];

}

return self;
}

そして、現在の最終結果の画像:

現在のセル 結果

ご覧のとおり、テキストフィールドは中央部分から始まっておらず、半分の幅でもありません。これは、テーブルビューがそれよりも小さい場合でも、常に幅が 320 であるためです (iphone 5 で実行している場合)。以上 (iphone 6)。

何か助けてください。ありがとうございました。

4

2 に答える 2

1

Antoineの答えは正しいと思いますが、CustomCellのサブビューに関する問題がある場合は、サブビューをレイアウトする必要があります. iOS 開発者ライブラリによると

layoutSubviews - サブビューのレイアウトを、制約または自動サイズ変更動作よりも正確に制御する必要がある場合は、このメソッドを実装します。

したがって、 UITableViewCell- (void)layoutSubviewsにメソッドを実装する必要があります

また、実装する必要があります

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

そうしないと、セルの高さが常にデフォルト値になります。

于 2015-05-22T11:41:52.153 に答える