OGeek|极客世界-中国程序员成长平台

标题: ios - 具有动态 UILabel 的 UITableViewCell 的动态高度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:35
标题: ios - 具有动态 UILabel 的 UITableViewCell 的动态高度

我有一个 UITableViewController,它显示了一个自定义 UITableViewCell(继承自 UITableViewCell)。同一个 UITableViewCell 有一个 UILabel 可以有可变长度的文本。我面临的挑战是如何在我的 UITableViewController 中访问这个 UILabel,以便我可以在 heightForRowAtIndexPath 中设置正确的单元格高度。

或者在旁注中,我该如何解决我的标签动态大小的问题。

谢谢

这是我自定义 UITableViewCell 的代码:

标题:

#import <UIKit/UIKit.h>

@interface MessagesCustomViewCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *message;   <---- need to access this 

@end

实现:

#import "MessagesCustomViewCell.h"

@implementation MessagesCustomViewCell
@synthesize message=_message;

- (id)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelectedBOOL)selected animatedBOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end



Best Answer-推荐答案


使用Ivan提到的测量字体大小的方法,您可以在MessagesCustomViewCell中额外添加一个类方法,

+ (CGFloat)heightForMessageNSString *)message;

您在其中使用适当的 UILabel 宽度/高度、字体等计算消息的高度。这可以从 heightForRowAtIndexPath: 中调用:

NSString *dynamicString = [self.mydata objectAtIndex:indexPath.row];
CGFloat height = [MessagesCustomViewCell heightForMessage:dynamicString];
return height;

关于ios - 具有动态 UILabel 的 UITableViewCell 的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125488/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4