希望这些屏幕截图有助于解释我的问题 - 这是我的 uitablecell 在第一次加载时的样子(注意单元格中的标题标签):
如果我在下面滚动并返回,它会变成正确的(不再省略,它应该是第一次加载的方式):
我正在计算动态标签大小并将其用作约束。以下是相关代码(以下是作为 IOS rubymotion 项目的一部分用 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高手赐教!