i有一个UaiteViewController,它显示一个自定义的UaiterViewCell(从uitaiteViewCell继承)。相同的UitableViewCell具有一个可以具有可变长度的文本的Uilabel。我面临的挑战是如何在我的Uitable ViewController中访问此uilabel,以便我可以在高度forrowAratIndExpath中设置正确的单元格高度。
或在旁注,我如何解决具有动态尺寸标签的问题。
谢谢
这是我的自定义uitaiteViewCell的代码:
标题:
#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提到的字体大小的方式,您可以在MessagsCustomViewCell中添加类Metod,
+ (CGFloat)heightForMessage:(NSString *)message;
使用适当的uilabel宽度/高度,字体等计算消息高度等。这可以从Height ForrowatIndExpath中调用:因此:
NSString *dynamicString = [self.mydata objectAtIndex:indexPath.row];
CGFloat height = [MessagesCustomViewCell heightForMessage:dynamicString];
return height;
呼叫[tableview reloaddata]一旦数据更新。
使用[yourstring sizewithfont :( uifont*)forwidth(cgfloat)linebreakmode :( nsline breactmode)]方法在height -forrowatex中。推荐方法将返回所需的大小(包括高度)。