因此,我以编程方式设置a中的frame
和 设置,然后使用自定义来填充数据。问题是自定义单元格始终具有320宽度和44的and ,即使我将其设置为.UITableView
UIViewController
UITableViewCell
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,即使 tableview 小于该宽度(在 iphone 5 上运行时的情况)或更大(iphone 6)。
请问有什么帮助吗?谢谢你。