0

希望这些屏幕截图有助于解释我的问题 - 这是我的 uitablecell 在第一次加载时的样子(注意单元格中的标题标签):

在此处输入图像描述

如果我在下面滚动并返回,它会变成正确的(不再省略,它应该是第一次加载的方式):

在此处输入图像描述

我正在计算动态标签大小并将其用作约束。以下是相关代码(以下是作为 IOS ruby​​motion 项目的一部分用 ruby​​ 编写的):

  def tableView(tableView, cellForRowAtIndexPath: indexPath)
    object = @posts[indexPath.row]
    cellIdentifier = "DayCell"
    cell = @day_table_view.dequeueReusableCellWithIdentifier(cellIdentifier)
    unless cell
      cell = PostCellView.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier: cellIdentifier)
      cell.contentView.apply_constraints   
    end
    cell
  end

  def tableView(tableView,willDisplayCell: cell, forRowAtIndexPath:indexPath)
    object = @posts[indexPath.row]
    cell.fill(object)
  end

post_cell_view.rb # 填充方法

def fill(post)
    self.post = post
    @title_label.text = post.title

    @reason_image_view.setImageWithURL(NSURL.URLWithString(post.reason.icon),
                   placeholderImage: nil,
                   completed: lambda{|image,error,cacheType|
                      # if cacheType == 0
                        @reason_image_view.alpha = 0
                        UIView.animateWithDuration(0.25, 
                          animations: lambda{
                              @reason_image_view.alpha = 1
                            })
                      # end
                    })

    expectedTitleLabelSize = @title_label.text.sizeWithFont(@title_label.font, constrainedToSize:[App.window.frame.size.width - 50, Float::MAX], lineBreakMode: NSLineBreakByWordWrapping)  
    @title_label.addConstraint Teacup::Constraint.new(@title_label, :height).equals(expectedTitleLabelSize.height).nslayoutconstraint

    @post_picture_image_view.setImageWithURL(NSURL.URLWithString(post.image.file_medium),
                   placeholderImage: nil,
                   completed: lambda{|image,error,cacheType|
                      # if cacheType == 0
                        @post_picture_image_view.alpha = 0
                        UIView.animateWithDuration(0.25, 
                          animations: lambda{
                              @post_picture_image_view.alpha = 1
                            })
                      # end
                  })
    @post_picture_image_view.contentMode = UIViewContentModeScaleAspectFill

    if post.cost.blank?
      @price_flag_image.alpha = 0
    else
      @price_flag_image.alpha = 1
      @price_label.text = post.cost 
    end

    @location_label.text = post.location.name
    @distance_label.text = post.location.distance.to_s

  end

我猜问题是我没有在正确的时间应用约束?请各位stackoverflow高手赐教!

4

2 回答 2

2

以下是我处理可变高度标签的方式:

  • 设置lineBreakModeNSLineBreakByWordWrapping
  • 设置numberOfLines为 0
  • 通过设置首选的最大布局宽度setPreferredMaxLayoutWidth:
  • 像往常一样设置自动布局约束

这是一个简化的要点,演示了 UITableView 和 UITableViewCell 子类的设置:

https://gist.github.com/dblandin/6303425

如果您只想下载并运行它,还有一个 zip:

https://dl.dropboxusercontent.com/u/1096930/dynamic-label.zip

我还更进一步,向您展示了如何设置可变高度 UITableViewCells。

从您的屏幕截图中,似乎所有单元格都设置为固定高度。

希望这可以帮助!如果您还有其他问题,请告诉我。

于 2013-08-22T05:17:12.527 回答
0

我能够通过删除@title_label 上的所有约束(我之前使用自动布局来定位标签)并将其转换为固定坐标来解决此问题。所以在填充()

expectedTitleLabelSize = @title_label.text.sizeWithFont(@title_label.font, constrainedToSize:[App.window.frame.size.width - 50, Float::MAX], lineBreakMode: NSLineBreakByWordWrapping)  
@title_label.frame = [[35,60],[270, expectedTitleLabelSize.height]]

这解决了我的问题 - 仍然想知道如何让它与约束一起工作。

于 2013-07-31T20:39:46.347 回答