1

I have subclassed UITableViewCell by only overriding the -(void)layoutSubviews method:

-(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];

}

Inside the cell, I have also added a UIStepper subview in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method:

- (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
}

However, when the text for textLabel is too long, it seems to squeeze away the detailTextLabel. I am wondering why this is happening as I have already specify the frames for these subviews. enter image description here

Thank you in advance!

6
  • 1
    I would suggest creating a custom Cell for this, instead of subclassing the currently available cell styles.
    – Lefteris
    Dec 30, 2013 at 23:07
  • Is the text in label using the text property? (If so maybe change to using the attributedText property and control the text behaviour with an NSAttributedString)
    – Bamsworld
    Dec 30, 2013 at 23:16
  • how is attributedText different from text? I am using text. @Bamsworld
    – Ra1nWarden
    Dec 30, 2013 at 23:35
  • That might work but what is wrong here? @Lefteris
    – Ra1nWarden
    Dec 30, 2013 at 23:36
  • 1
    Attributed strings give you a lot more control over the appearance of a string. However, looking at your code more in depth I'm curious as to the frames (origin and size) of textLabel and detailTextLabel @Ra1nWarden
    – Bamsworld
    Dec 31, 2013 at 0:19

2 Answers 2

1

Ok, I have solved the problem by manually setting the CGRectfor the frame:

-(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];

}

Thanks to @Bamsworld.

0
0

As speculated in the comments [super layoutSubviews] is altering the frames of your labels and you are only changing the width.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.