0

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

请问有什么帮助吗?谢谢你。

4

2 回答 2

1

您可以使用委托方法来设置高度:

// For swift:
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

// For Objective-C
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
于 2015-05-22T09:49:58.993 回答
1

我认为 Antoine 的回答是正确的,但是如果您对CustomCell 的子视图有问题,您需要布局子视图。根据iOS 开发者库

layoutSubviews - 如果您需要比约束或自动调整大小行为提供的更精确的子视图布局控制,请实施此方法。

所以你需要在你的UITableViewCell- (void)layoutSubviews中实现方法

您还应该实施

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

如果你不这样做,你的单元格高度总是成为默认值。

于 2015-05-22T11:41:52.153 回答