【问题标题】:UILabel dynamic height within UITableviewCell acts erraticUITableviewCell 中的 UILabel 动态高度行为不稳定
【发布时间】:2013-07-31 01:08:59
【问题描述】:

希望这些屏幕截图有助于解释我的问题 - 这是我的 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高手赐教!

【问题讨论】:

  • 我一定错过了什么。这两张图片在我看来是一样的。
  • 在第一张图片中,标题标签是椭圆形的 - uilabel 的高度没有正确设置。在第二张图片中,您可以看到它已正确设置
  • 您是否尝试过将单元格填入tableView(tableView, cellForRowAtIndexPath: indexPath),而不是willDisplayCell
  • 是的,我确实尝试过 - 仍然是同样的问题

标签: iphone ios autolayout rubymotion


【解决方案1】:

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

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

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

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

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

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

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

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

希望这会有所帮助!如果您有任何其他问题,请告诉我。

【讨论】:

  • 哦,太好了,感谢您的详细回答-我会检查一下
【解决方案2】:

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

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]]

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

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多