Unable add UIVIew to UITableView Cell Programmatically

| | August 6, 2015

I am trying to create a Card Based News feed (Similar to Facebook app) in iOS.I’m implementing programmatically because the height of UITablecell has to increase/decrease based on the data.

Consider i have one UIlabel and one UIImageView added, then the height of UITablecell should be adjusted based on the fields. If one more text feild is added then height has to increase and if imageview is removed then height has to decrease.

The issue here is I couldn’t to add CustomView on the UItablecell programmatically but when i create using Interface builder then its working fine but the height remains constant which i don’t want to have.

Can some one please help me out.


   @interface TableViewCell : UITableViewCell

  @property (strong,nonatomic) UIView *customView;
  @property (strong,nonatomic) UILabel *customLabel;
  @property (strong,nonatomic) UIImageView *customImage;



 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString            *)reuseIdentifier
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {
    // configure control(s)

    [self addSubview:self.customView];
    [self.customView addSubview:self.customLabel];
    [self.customView addSubview:self.customImage];

return self;

  - (UIView *)customView
   if (!_customView)
    _customView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.contentView.frame.size.width, 50)];
    [_customView setTranslatesAutoresizingMaskIntoConstraints:NO];

   return _customView;


 - (UIImageView *)customImage
     if (!_customImage)
      _customImage = [UIImageView new];
      _customImage.clipsToBounds = YES;
      [_customImage.layer setBorderColor: [[UIColor grayColor] CGColor]];
      [_customImage.layer setBorderWidth: 1.0];
      _customImage.contentMode = UIViewContentModeCenter;
      [_customImage setTranslatesAutoresizingMaskIntoConstraints:NO];

   return _customImage;

 - (UILabel *)customLabel
    if (!_customLabel)
      _customLabel = [UILabel new];
      [_customLabel setTranslatesAutoresizingMaskIntoConstraints:NO];


   return _customLabel;


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{

   if (!_stubCell)
      _stubCell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];


    CGFloat height =  [_stubCell.contentView   systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
   return height + 1;

  - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  return 50.f;

   - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
 static NSString *cellIdentifier = @"TableCell";

// Similar to UITableViewCell, but
_stubCell = (TableViewCell *)[theTableView       dequeueReusableCellWithIdentifier:cellIdentifier];
if (_stubCell == nil) {
    _stubCell = [[TableViewCell alloc]     initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

// Just want to test, so I hardcode the data
  _stubCell.customLabel.text = @"Testing";

  return cell;

One Response to “Unable add UIVIew to UITableView Cell Programmatically”

  1. I think it may be better to create multiple nibs for the same class UITableViewCell, one for each of the possible layouts that you need. Depending on which nib is used then set heightForRowAtIndexPath to the height of the nib.

    This will give you predictability of the layout for each situation and you connect only the fields defined in UITableViewCell in cellForRowAtIndexPath.

Leave a Reply