Search code examples
iosuitableviewdetailtextlabel

UITableViewCell's detailTextLabel not use all the available space


As shown in the screenshot

As shown in the screenshot. Just wondering how can I ask the detailTextLabel to make use all of the available space, say, the area of the red rect. I tried to set the frame of the detailTextLabel to make the width bigger which just doesn't work.

Thank you in advance.


Solution

  • I'd recommend you to subclass a UITableViewCell and put all your cell customization in it. It could look similar to this code:

    CustomCell.h

    @interface CustomCell : UITableViewCell
    
    - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier;
    
    @end
    

    CustomCell.m

    #import "CustomCell.h"
    
    @implementation CustomCell
    
    - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
        if (self)
        {
            // Customization not related to positions and sizes of subviews. For example:
            self.detailTextLabel.textColor = [UIColor lightGrayColor];
            self.detailTextLabel.text = @"Aaaaaa…";
        }
    
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // Customization related to positions and sizes of subviews. For example:
        self.detailTextLabel.frame = CGRectMake(10.0, 10.0, 240.0, 40.0);
    }
    
    @end