ios - UITableViewCell显示问题

标签 ios uitableview

编辑:与 NSAttributedString、OHAttributedLabel 或 TTTAttributedLabel 无关!

简短的问题:我使用UILabel、 OHAttributedLabel 或TTTAttributedLabel 遇到同样的问题 - 滚动表格 View 会破坏标签的显示。更多内容如下...

长格式: 我有一个 NSAttributedString 对象,表示推文中的文本。我根据推文中的各种内容装饰了字符串(例如,主题标签为蓝色,网址为绿色并带下划线,用户提及为红色)。

例如,这条推文:

 @MeetGreen Keep up the good work. Say Hi to Nancy for me - she convinced me that I can be green and save green at the same time!

此 NSAttributedString 中的结果:(使用我编写的实用程序显示,以帮助我了解发生了什么):

@MeetGreen{
    NSColor = "UIDeviceRGBColorSpace 1 0 0 1";
    NSFont = "<UICFFont: 0x93c57d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 17px";
} Keep up the good work. Say Hi to Nancy for me - she convinced me that I can be green and save green at the same time!{
    CTForegroundColor = "<CGColor 0x93c8f20> [<CGColorSpace 0x938b570> (kCGColorSpaceDeviceGray)] ( 0 1 )";
    NSFont = "<UICFFont: 0x93c57d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 17px";
}

生成 NSAttributedString 对象的代码似乎正在执行我想要它执行的操作,但是当我尝试在 UILabel(或 TTTAttributedLabel 或 OHAttributedLabel)中显示该对象时,我无法说服它正常工作。主要问题似乎是滚动行为 - 单元格未正确绘制(文本位于某个奇怪的位置)。但是,如果我只是选择该单元格(例如,通过触摸它),该单元格就会按照应有的样子准确绘制。这让我认为问题在于我的委托(delegate)方法中的表格滚动处理,而不是属性标签本身(因为 OHAttributedLabel 和 TTTAttributedLabel 的行为相同),但我不确定为什么会这样。 (我还没有尝试过的一件事是推文内容的纯文本和纯 UILabel - 这在今晚的列表中。好吧,UILabel 和纯文本内容的行为相同。我做错了什么??? )

这是我的 tableView:cellForRowAtIndexPath:、tableView:heightForRowAtIndexPath: 和 tableView:didSelectRowAtIndexPath:; 的代码;请注意,此代码驻留在 UIViewController 子类中,而不是 UITableViewController 的子类中。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TweetingTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Tweet"];

    TSTweet *tweet = [self.model tweetAtIndexPath:indexPath];

    cell.userHandleLabel.text = tweet.user.screenName;
    cell.userNameLabel.text = tweet.user.name;
    cell.timestampLabel.text = timeStamp;
    cell.tweetTextLabel.attributedText = [self.model decoratedStringForTweetAtIndexPath:indexPath];

    UIImage *image = [UIImage imageNamed:@"avatar"];

    cell.userImage.image = image;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat tweetHeight = [self.model heightForTweetAtIndexPath:indexPath
                                                     usingWidth:label_width];

    CGFloat height = tweetHeight + label_headroom + label_footroom;

    CGFloat result = MAX(height,defaultHeight);

    NSLog(@"[%@ %@] %@ (%@)\ntweetHeight=%f, height=%f, defaultHeight=%f, result=%f", NSStringFromClass([self class]), NSStringFromSelector(_cmd),indexPath,[self.model decoratedStringForTweetAtIndexPath:indexPath].string,tweetHeight,height,defaultHeight,result);

    return result;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"[%@ %@] indexPath=%@", NSStringFromClass([self class]), NSStringFromSelector(_cmd),indexPath);

    TweetingTweetCell *cell = (TweetingTweetCell *)[tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"decoratedString=%@",[self.model decoratedStringForTweetAtIndexPath:indexPath]);
    [cell dumpDebugInfo];
    [tableView deselectRowAtIndexPath:indexPath
                             animated:NO];
}

TweetingTweetCell 是 UITableViewCell 的子类,包含 4 个对象:一个用于头像的 UIImageView、一个用于发送者屏幕名称的 UILabel、一个用于发送时间的 UILabel 以及一个用于推文内容的“标签”对象。正如我上面所说,我尝试过 OHAttributedLabel 和 TTTAttributedLabel,行为是相同的。

滚动不良:

strange scroll

  • 文本和屏幕名称之间的所有空白都很奇怪。

选择期间:

fixed by selection

  • 查看定位是如何固定的

最佳答案

已解决 - 飞行员错误。问题在于 Spring /支柱的邪恶阴谋以及我如何设置标签的大小。

上面的 tableView:heightForRowAtIndexPath: 代码按预期工作。我做了两项影响结果行为的更改:

  1. 我在tableView:cellForRowAtIndexPath:中添加了代码来设置tweetTextLabel的高度,即:

    CGRect rect = cell.tweetTextLabel.frame;
    rect.size.height = [self.model heightForTweetAtIndexPath:indexPath
                                                  usingWidth:label_width];
    cell.tweetTextLabel.frame = rect;
    
  2. 我关闭了右侧和底部支柱以及 tweetTextLabel 上的垂直 Spring (在 IB 中)。

这两项更改使初始演示和滚动行为正确。

关于ios - UITableViewCell显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078542/

相关文章:

ios - UInt8 XOR 数组结果到 NSString 的转换每次都返回 nil

ios - 在自定义 UITableViewCell 中访问 subview

ios - tableviewcell 中的图像不会立即出现

ios - UutableView 选定的单元格到其他 View

ios - 原型(prototype) UITableViewCell 初始化

ios - 在从服务器加载图像之前加载 "Loading"图像?

ios - Apple Watch 让应用保持活跃几秒钟

ios - 如何在不使用 sender.tag 方法的情况下管理 UItableViewCell 中的 UIbutton?

ios - 如何在 iOS 中的 Whatsapp 上分享视频?

iphone - Objective C/Iphone开发中Perform Selector有什么用