简体   繁体   中英

dynamic cell height for custom uitableviewcell not working

I created a custom tableview cell and the code is given below. I am trying to add a new UIView named bottomTextContainer to the contentview of the cell.The bottomTextContainer holds the labels of published date and source of the article. my problem is bottomTextContainer view is not showing inside the cell it is overlapping with the next cell.

ArticleCell.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ArticleCell : UITableViewCell

#pragma mark- Properties

@property (strong, nonatomic) UILabel *source;
@property (strong, nonatomic) UILabel *publishedDate;

#pragma mark- Methods

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier;

@end

NS_ASSUME_NONNULL_END

ArticleCell.m


#import "ArticleCell.h"

@interface ArticleCell ()

#pragma mark- Private Properties

@property (strong, nonatomic) UIView *bottomTextContainer;

@end

@implementation ArticleCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    
    self.textLabel.numberOfLines = 0;
    self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    self.textLabel.textColor = [UIColor blackColor];
    self.textLabel.textAlignment = NSTextAlignmentLeft;

    self.detailTextLabel.numberOfLines = 0;
    self.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16];
    self.detailTextLabel.textColor = [UIColor blackColor];
    self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
    
    self.bottomTextContainer = [[UIView alloc] init];
    [self.contentView addSubview:self.bottomTextContainer];
    self.bottomTextContainer.translatesAutoresizingMaskIntoConstraints = false;
    
    self.source = [[UILabel alloc] init];
    [self.bottomTextContainer addSubview:self.source];
    self.source.textColor = [UIColor blackColor];
    self.source.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    self.source.textAlignment = NSTextAlignmentLeft;
    self.source.translatesAutoresizingMaskIntoConstraints = false;
    
    self.publishedDate = [[UILabel alloc] init];
    [self.bottomTextContainer addSubview:self.publishedDate];
    self.publishedDate.textColor = [UIColor blackColor];
    self.publishedDate.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    self.publishedDate.textAlignment = NSTextAlignmentRight;
    self.publishedDate.translatesAutoresizingMaskIntoConstraints = false;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    UILayoutGuide *contentViewLayout =  self.contentView.layoutMarginsGuide;
    
    [self.bottomTextContainer.topAnchor constraintEqualToAnchor:self.detailTextLabel.bottomAnchor].active = YES;
    [self.bottomTextContainer.leadingAnchor constraintEqualToAnchor:contentViewLayout.leadingAnchor].active = YES;
    [self.bottomTextContainer.trailingAnchor constraintEqualToAnchor:contentViewLayout.trailingAnchor].active = YES;
    
    [self.source.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
    [self.source.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
    [self.source.leadingAnchor constraintEqualToAnchor:self.bottomTextContainer.leadingAnchor].active = YES;
    [self.source.widthAnchor constraintEqualToAnchor:self.bottomTextContainer.widthAnchor multiplier:0.5].active = YES;
    
    [self.publishedDate.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
    [self.publishedDate.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
    [self.publishedDate.trailingAnchor constraintEqualToAnchor:self.bottomTextContainer.trailingAnchor].active = YES;
    [self.publishedDate.leadingAnchor constraintEqualToAnchor:self.source.trailingAnchor].active = YES;
}

@end

This is the result I am getting. Show

Try this:

tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM