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.
Thank you in advance!
As speculated in the comments [super layoutSubviews] is altering the frames of your labels and you are only changing the width.