AutoUILabels UITableViewCell

I add my own UILabelin contentViewfor UITableViewCell, because I need more control over the layout than the default UITableViewCellStyles provides. Essentially, I want detailLabel to take precedence over textLabel, so textLabel is truncated.

I have the following code in mine UITableViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView
  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString * const kCellIdentifier = @"CustomCell";

  UITableViewCell * cell =
      [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

  UILabel * titleLabel, * dateLabel;

  if(cell == nil)
  {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:kCellIdentifier] autorelease];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    titleLabel = [[[UILabel alloc] init] autorelease];
    titleLabel.tag = kTitleLabelTag;
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    dateLabel = [[[UILabel alloc] init] autorelease];
    dateLabel.tag = kDateLabelTag;
    dateLabel.textColor = [UIColor blueColor];
    dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

    [cell.contentView addSubview:titleLabel];
    [cell.contentView addSubview:dateLabel];
  }

  [self configureCell:cell atIndexPath:indexPath];
  return cell;
}

- (void)configureCell:(UITableViewCell *)pCell
  atIndexPath:(NSIndexPath *)pIndexPath
{
  const float kHeight = 44.0, kLeftIndent = 8.0, kOverallWidth = 293.0,
      kGap = 1.0;

  UILabel * titleLabel, * dateLabel;
  titleLabel = (UILabel *)[pCell.contentView viewWithTag:kTitleLabelTag];
  dateLabel = (UILabel *)[pCell.contentView viewWithTag:kDateLabelTag];

  NSString * dateText = @"9:39 AM";

  // Calculate the size of dateLabel
  CGSize dateSize = [dateText sizeWithFont:[dateLabel font]
      constrainedToSize:CGSizeMake(kOverallWidth, kHeight)];

  const float dateXPos = kOverallWidth - dateSize.width;
  dateLabel.frame = CGRectMake(dateXPos, 0.0, dateSize.width, kHeight);
  titleLabel.frame = CGRectMake(kLeftIndent, 0.0,
      dateXPos - kLeftIndent - kGap, kHeight);

  titleLabel.text = @"Some potentially very long text which will be wrapped.";
  dateLabel.text = dateText;
  pCell.contentView.backgroundColor = [UIColor purpleColor];
}

The code above gives incorrect results. When the table view is initially shown, it looks like Figure 1) in this rendering image .

Thus, there is an undesirable gap on the right of all dateLabels. (purple background just for better visibility of what is happening)

, 2), 3).

, , configureCell:atIndexPath:. , , , .

, - , setNeedsLayout layoutSubviews pCell pCell.contentView, .

autoresizingMask titleLabel dateLabel UIViewAutoresizingNone, , , , , dateLabel.

, ?

!

PS: , , , .

+3
2

, , :

UITableViewCell, Interface Builder. "MyTableViewCell" , CellForRowAtIndexPath :

MyTableViewCellClass *cell = (MyTableViewCellClass *)[tableView dequeueReusableCellWithIdentifier:@"MyTableViewCellClass"] autorelease];

if (!cell)
cell = [[[MyTableViewCellClass alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"MyTableViewCellClass"] autorelease];

// Call specific methods on your cell to pass information to it, not for display
[cell setProperties:...];

layoutSubviews UITableViewCell. :

-(void) layoutSubviews
{
// You must call this first to make sure your cell gets current parent information
[super layoutSubviews];

// Retrieve the content view bounds. This will include the edit symbols when present (delete button and ordering symbol
float inset = 5.0;
CGRect bounds = [[self contentView] bounds];

// Keep on going here with your own view layout.

}

, (CellForRowAtIndexPath) ( ). () , , , CellForRowAtIndexPath.

+3

, " " !

autoresizesSubviews

- (void)awakeFromNib
{
    // Initialization code
    self.autoresizesSubviews=YES;

}

- , layoutSubviews UITableViewCell

-(void)layoutSubviews{

    [super layoutSubviews];
    CGFloat top=VerticalPadding;
    CGFloat left=HorizentalPadding;
    CGFloat width=CGRectGetWidth(self.frame)-2*HorizentalPadding;
    CGFloat height=CGRectGetHeight(self.frame)-2*VerticalPadding;
    CGRect rect=CGRectMake(left, top, width, height);
    self.Label.frame=rect;

}
+1

Source: https://habr.com/ru/post/1794161/


All Articles