ios - 具有动态 UILabel 的 UITableViewCell 的动态高度

标签 ios ios5 uitableview ios6

我有一个 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)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

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

    // Configure the view for the selected state
}

@end

最佳答案

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

+ (CGFloat)heightForMessage:(NSString *)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/

相关文章:

ios - 当表格 View 被多重选择时,使用自定义图像替换复选标记

iOS:使用你的 Appdelegate 作为小东西的模型是好的设计模式吗?

ios - 以编程方式发送/接收数据短信iOS

ios - 如何使用 "hide"框架

objective-c - 设置自定义 UIView 实例的属性时出现无法识别的选择器错误

ios - UIScrollViews 并在其上添加组件

iphone - UIAlertViewDelegate 类 "self"实例在按下按钮之前被释放

objective-c - iOS 7中UIViewController中NavBar和TabBar下UITableView的边缘延伸以获得半透明效果

json - 在TableView上执行搜索后,应用程序崩溃

ios - 删除 NSMutableAttributedString 中的特定文本