【问题标题】:dynamic cell height for custom uitableviewcell not working自定义 uitableviewcell 的动态单元格高度不起作用
【发布时间】:2021-08-09 15:53:29
【问题描述】:

我创建了一个自定义表格视图单元格,代码如下所示。我正在尝试向单元格的 contentview 添加一个名为 bottomTextContainer 的新 UIView。bottomTextContainer 包含文章发布日期和来源的标签。我的问题是 bottomTextContainer 视图未显示在与下一个单元格重叠的单元格内。

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

这是我得到的结果。 Show

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我尝试过使用它,也尝试过使用表格视图委托方法,但没有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2011-11-27
    • 2023-04-02
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多