ios - 在 UITableView 页脚中动态设置和调整标签大小

标签 ios uitableview rubymotion tablefooterview

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

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 是问题的一部分?

最佳答案

我以为我已经尝试过了,但也许我的订单已关闭。不管怎样,我通过在 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

关于ios - 在 UITableView 页脚中动态设置和调整标签大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40070911/

相关文章:

ios - 如何在没有要 resignFirstResponder 的文本字段时关闭键盘

ios - 我如何在 Rubymotion 中编写此 C 代码?

objective-c - 如何在 RubyMotion 中实现 OSX 上的图像按钮?

iOS - 使用动画展开/折叠 UITableView 部分

ios - 捕获并停止传播用户交互 iOS UITableViewCell

ios - RubyMotion UIScrollView 不滚动

jquery - 改变 jQuery 中 div 的字体大小

ios - 如何在 iOS 8 中获取第三方键盘大小?

ios - fabric crashlytics 日志中缺少行号问题

ios - 截图并以编程方式发送