1

UITableViewCell仅通过覆盖该-(void)layoutSubviews方法进行了子类化:

-(void)layoutSubviews {
    [super layoutSubviews];  //The default implementation of the layoutSubviews
    
    CGRect textLabelFrame = self.textLabel.frame;
    textLabelFrame.size.width = 180;
    self.textLabel.frame = textLabelFrame;
    self.textLabel.textAlignment = NSTextAlignmentLeft;
    self.textLabel.adjustsFontSizeToFitWidth = YES;
    
    CGRect detailTextLabelFrame = self.detailTextLabel.frame;
    detailTextLabelFrame.size.width = 30;
    self.detailTextLabel.frame = detailTextLabelFrame;
    self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
    self.detailTextLabel.adjustsFontSizeToFitWidth = YES;
    
    [self sizeToFit];

}

在单元格内,我还在方法中添加了一个UIStepper子视图- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// code omitted   
    UITableViewCell *cell;
    if(indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:OrderCellIdentifier forIndexPath:indexPath];
        if ( cell == nil ) {
            cell = [[GinkgoDeliveryOrderCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:OrderCellIdentifier];
            UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(113, 7, 94, 29)];
            [cell addSubview:stepper];
        }
// code omitted
}

但是,当文本textLabel太长时,它似乎会挤掉detailTextLabel. 我想知道为什么会发生这种情况,因为我已经为这些子视图指定了框架。 在此处输入图像描述

先感谢您!

4

2 回答 2

1

CGRect好的,我已经通过手动设置框架解决了这个问题:

-(void)layoutSubviews {
    [super layoutSubviews];  //The default implementation of the layoutSubviews

    self.textLabel.frame = CGRectMake(15, 11, 180, 21);
    self.textLabel.textAlignment = NSTextAlignmentLeft;
    self.textLabel.adjustsFontSizeToFitWidth = YES;

    CGRect detailTextLabelFrame = CGRectMake(280, 11, 40, 21);
    self.detailTextLabel.frame = detailTextLabelFrame;
    self.detailTextLabel.textAlignment = NSTextAlignmentCenter;

    [self sizeToFit];

}

感谢@Bamsworld。

于 2013-12-31T01:14:14.777 回答
0

正如评论中推测的那样 [super layoutSubviews] 正在改变标签的框架,而您只是在改变宽度。

于 2013-12-31T01:19:12.250 回答