TTTableViewCell 中使用 TTStyledTextLabel 的布局混乱

发布于 2024-11-09 19:15:19 字数 2314 浏览 1 评论 0原文

这是我第一次使用 Three20,我尝试使用以下代码将 TTStyledTextLabel 添加到我的 TTTableViewCell:

@interface ConvoreCell : TTTableViewCell{
    TTStyledTextLabel * tt_title;
    UITextView * title;
    UILabel * detailed;
}

@property (nonatomic, retain) IBOutlet UITextView * title;
@property (nonatomic, retain) IBOutlet UILabel * detailed;
@property (nonatomic, retain) TTStyledTextLabel * tt_title;

@end



- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
        // Initialization code
        tt_title = [[TTStyledTextLabel alloc] init];
        tt_title.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:tt_title];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame = tt_title.frame;
    frame.size.width = 640;
    frame.size.height = tt_title.text.height;
    frame.origin.x = 45;
    frame.origin.y = 5;
    tt_title.frame = frame;
}


and in my TTTableView I have:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat titleHeight = [TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:YES URLs:YES].height;
    //NSLog(@"HEIGHT IS %f", titleHeight);
    return titleHeight + 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ConvoreCell";

    ConvoreCell *cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[ConvoreCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

 TTImageView * avatar = [[TTImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+5, cell.frame.origin.y+5, 40, 40)];
        avatar.urlPath = [[[self.posts objectAtIndex:indexPath.row] creator] img];
        avatar.userInteractionEnabled = YES;
        avatar.tag = indexPath.row;
        [cell addSubview:avatar];


 cell.tt_title.text = [TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:YES URLs:YES];
}

但是,布局非常混乱。这是为什么呢? 在此处输入图像描述

This is my first time using Three20 and I am trying to add a TTStyledTextLabel to my TTTableViewCell using the following code:

@interface ConvoreCell : TTTableViewCell{
    TTStyledTextLabel * tt_title;
    UITextView * title;
    UILabel * detailed;
}

@property (nonatomic, retain) IBOutlet UITextView * title;
@property (nonatomic, retain) IBOutlet UILabel * detailed;
@property (nonatomic, retain) TTStyledTextLabel * tt_title;

@end



- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
        // Initialization code
        tt_title = [[TTStyledTextLabel alloc] init];
        tt_title.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:tt_title];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame = tt_title.frame;
    frame.size.width = 640;
    frame.size.height = tt_title.text.height;
    frame.origin.x = 45;
    frame.origin.y = 5;
    tt_title.frame = frame;
}


and in my TTTableView I have:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat titleHeight = [TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:YES URLs:YES].height;
    //NSLog(@"HEIGHT IS %f", titleHeight);
    return titleHeight + 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ConvoreCell";

    ConvoreCell *cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[ConvoreCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

 TTImageView * avatar = [[TTImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+5, cell.frame.origin.y+5, 40, 40)];
        avatar.urlPath = [[[self.posts objectAtIndex:indexPath.row] creator] img];
        avatar.userInteractionEnabled = YES;
        avatar.tag = indexPath.row;
        [cell addSubview:avatar];


 cell.tt_title.text = [TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:YES URLs:YES];
}

However, the layout is messy as hell. Why is this?
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2024-11-16 19:15:19

我找不到你的代码有什么问题。

看着图像,让我印象深刻的是正确显示的最后一行包含一个链接。

我怀疑这可能是 TTStyledText 中的一些错误,根据我的经验,它并不适合显示完整的 HTML。

你能尝试证实(或驳回)这个假设吗?总是这样,连续的一个url就会让它“发疯”吗?

无论如何,在该行上设置断点(或之后的 NSLog )

 cell.tt_title.text = [TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:YES URLs:YES];

并检查 cell.tt_title.text 中发生的情况都会有所帮助。

I cannot find anything wrong with your code.

Looking at the image, what strikes me is that the last row which is correcly displayed contains a link.

What I suspect is that it could be some bug in TTStyledText, which in my experience is not really suitable for displaying full HTML.

Could you try and confirm (or dismiss) this hypothesis? Is always so that a url in a row will make it "go crazy"?

In any case, it will help to set a breakpoint on (or an NSLog after) the line

 cell.tt_title.text = [TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:YES URLs:YES];

and check what is going in cell.tt_title.text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文