2

在我的应用程序中,当我选择一个单元格时,我会动态更新表格视图的页脚文本。不幸的是,我这样做的方式似乎改变了UILabel我的UIView. 例如,当我点击一个单元格时,我的标签宽度变为 950 到 1200 之间。实际上,宽度应该(最多)是视图的宽度。这是我的代码:

def tableView(tableView, viewForFooterInSection:section)
  (section != (self.sections.count - 1)) ? footer_view : nil
end

def tableView(tableView, heightForFooterInSection: section)
  if section != (self.sections.count - 1)
    footer_label.frame.size.height + 20.0
  else
    1.0
  end
end

def tableView(tableView, didSelectRowAtIndexPath: indexPath)
  tableView.beginUpdates
  footer_label.text = "Details: #{updated_details}"
  footer_label.sizeToFit
  tableView.endUpdates
end


def footer_view
  @footer_view ||= UIView.alloc.initWithFrame(CGRectZero).tap do |fv|
    fv.addSubview(footer_label)
  end
end

def footer_label
  @footer_label ||= UILabel.alloc.initWithFrame(CGRectZero).tap do |pl|
    pl.frame = CGRect.new(
      [15, 10],
      [self.view.frame.size.width - 30, self.view.frame.size.height]
    )
    pl.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption2)
    pl.numberOfLines = 0
    pl.lineBreakMode = NSLineBreakByWordWrapping
    pl.text          = ""
    pl.sizeToFit
  end
end

我猜打电话sizeToFit是问题的一部分?

4

2 回答 2

2

我以为我已经尝试过了,但也许我的订单已关闭。beginUpdates无论哪种方式,我通过在/内再次设置页脚的框架来解决它endUpdates

tableView.beginUpdates
frame = footer_label.frame
footer_label.text = "Plan Details: #{updated_details}"
frame.size.width = self.view.frame.size.width - 30
footer_label.frame = frame
footer_label.sizeToFit
tableView.endUpdates
于 2016-10-16T19:31:27.220 回答
1

我认为您需要为页脚使用动态高度。

tableView.heightForFooterInSection = UITableViewAutomaticDimension
tableView.estimatedSectionFooterHeight = 25.0

请记住,您必须设置 UILabel 的约束。也许您必须更新该部分以更改高度。

于 2016-10-16T13:48:56.437 回答