そのため、プログラムで のframe
と の設定を設定UITableView
しUIViewController
、カスタムを使用しUITableViewCell
てデータを入力しています。問題は、カスタム セルを で設定しても、幅が320で44のアンドが常にあることです。frame
contentView
height
initWithFrame
私のコード:
テーブルの作成:
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)。
何か助けてください。ありがとうございました。