<small id='vXYq9'></small><noframes id='vXYq9'>

      <i id='vXYq9'><tr id='vXYq9'><dt id='vXYq9'><q id='vXYq9'><span id='vXYq9'><b id='vXYq9'><form id='vXYq9'><ins id='vXYq9'></ins><ul id='vXYq9'></ul><sub id='vXYq9'></sub></form><legend id='vXYq9'></legend><bdo id='vXYq9'><pre id='vXYq9'><center id='vXYq9'></center></pre></bdo></b><th id='vXYq9'></th></span></q></dt></tr></i><div id='vXYq9'><tfoot id='vXYq9'></tfoot><dl id='vXYq9'><fieldset id='vXYq9'></fieldset></dl></div>
      1. <legend id='vXYq9'><style id='vXYq9'><dir id='vXYq9'><q id='vXYq9'></q></dir></style></legend>
      2. <tfoot id='vXYq9'></tfoot>
        • <bdo id='vXYq9'></bdo><ul id='vXYq9'></ul>

        使用 UIStackView 的动态 UITableView 行高?

        Dynamic UITableView row height using UIStackView?(使用 UIStackView 的动态 UITableView 行高?)
        <tfoot id='LT55Q'></tfoot>

        <i id='LT55Q'><tr id='LT55Q'><dt id='LT55Q'><q id='LT55Q'><span id='LT55Q'><b id='LT55Q'><form id='LT55Q'><ins id='LT55Q'></ins><ul id='LT55Q'></ul><sub id='LT55Q'></sub></form><legend id='LT55Q'></legend><bdo id='LT55Q'><pre id='LT55Q'><center id='LT55Q'></center></pre></bdo></b><th id='LT55Q'></th></span></q></dt></tr></i><div id='LT55Q'><tfoot id='LT55Q'></tfoot><dl id='LT55Q'><fieldset id='LT55Q'></fieldset></dl></div>
          <tbody id='LT55Q'></tbody>

            <bdo id='LT55Q'></bdo><ul id='LT55Q'></ul>
              • <legend id='LT55Q'><style id='LT55Q'><dir id='LT55Q'><q id='LT55Q'></q></dir></style></legend>

                <small id='LT55Q'></small><noframes id='LT55Q'>

                  本文介绍了使用 UIStackView 的动态 UITableView 行高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  很惊讶这不是开箱即用的,因为这似乎是堆栈视图的一个重要用例.我有一个 UITableViewCell 子类,它将 UIStackView 添加到 contentView.我在 tableView(_cellForRowAtIndexPath:) 中向堆栈视图添加标签,并且 tableview 设置为使用动态行高,但它似乎不起作用,至少在 Xcode 7.3 中.我也觉得在堆栈视图中隐藏排列的子视图是可动画的,但这似乎也被打破了.

                  Surprised this isn't working out of the box, as this seems to be an important use case for stack views. I have a UITableViewCell subclass which adds a UIStackView to the contentView. I'm adding labels to the stack view in tableView(_cellForRowAtIndexPath:) and the tableview is set to use dynamic row heights, but it doesn't appear to work, at least in Xcode 7.3. I was also under the impression that hiding arranged subviews in a stack view was animatable, but that seems broken as well.

                  关于如何使其正常工作的任何想法?

                  Any ideas on how to get this working correctly?

                  class StackCell : UITableViewCell {
                      enum VisualFormat: String {
                          case HorizontalStackViewFormat = "H:|[stackView]|"
                          case VerticalStackViewFormat = "V:|[stackView(>=44)]|"
                      }
                  
                      var hasSetupConstraints = false
                      lazy var stackView : UIStackView! = {
                          let stack = UIStackView()
                          stack.axis = .Vertical
                          stack.distribution = .FillProportionally
                          stack.alignment = .Fill
                          stack.spacing = 3.0
                          stack.translatesAutoresizingMaskIntoConstraints = false
                          return stack
                      }()
                  
                      override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
                          super.init(style: style, reuseIdentifier: reuseIdentifier)
                          contentView.addSubview(stackView)
                      }
                  
                      required init?(coder aDecoder: NSCoder) {
                          fatalError("init(coder:) has not been implemented")
                      }
                  
                      override func updateConstraints() {
                          if !hasSetupConstraints {
                              hasSetupConstraints = true
                              let viewsDictionary: [String:AnyObject] = ["stackView" : stackView]
                              var newConstraints = [NSLayoutConstraint]()
                              newConstraints += self.newConstraints(VisualFormat.HorizontalStackViewFormat.rawValue, viewsDictionary: viewsDictionary)
                              newConstraints += self.newConstraints(VisualFormat.VerticalStackViewFormat.rawValue, viewsDictionary: viewsDictionary)
                              addConstraints(newConstraints)
                          }
                          super.updateConstraints()
                      }
                  
                      private func newConstraints(visualFormat: String, viewsDictionary: [String:AnyObject]) -> [NSLayoutConstraint] {
                          return NSLayoutConstraint.constraintsWithVisualFormat(visualFormat, options: [], metrics: nil, views: viewsDictionary)
                      }
                  
                  class ViewController: UITableViewController {
                  
                      private let reuseIdentifier = "StackCell"
                      private let cellClass = StackCell.self
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                          configureTableView(self.tableView)
                      }
                  
                      private func configureTableView(tableView: UITableView) {
                          tableView.registerClass(cellClass, forCellReuseIdentifier: reuseIdentifier)
                          tableView.separatorStyle = .SingleLine
                          tableView.estimatedRowHeight = 88
                          tableView.rowHeight = UITableViewAutomaticDimension
                      }
                  
                      private func newLabel(title: String) -> UILabel {
                          let label = UILabel()
                          label.text = title
                          return label
                      }
                  
                      // MARK: - UITableView
                      override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
                          return 4
                      }
                  
                      override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                          return 44.0
                      }
                  
                      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                          return 10
                      }
                  
                      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                          let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StackCell
                          cell.stackView.arrangedSubviews.forEach({$0.removeFromSuperview()})
                          cell.stackView.addArrangedSubview(newLabel("(indexPath.section)-(indexPath.row)"))
                          cell.stackView.addArrangedSubview(newLabel("Second Label"))
                          cell.stackView.addArrangedSubview(newLabel("Third Label"))
                          cell.stackView.addArrangedSubview(newLabel("Fourth Label"))
                          cell.stackView.addArrangedSubview(newLabel("Fifth Label"))
                          return cell
                      }
                  
                      override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
                          let cell = tableView.cellForRowAtIndexPath(indexPath) as! StackCell
                          for (idx, view) in cell.stackView.arrangedSubviews.enumerate() {
                              if idx == 0 {
                                  continue
                              }
                              view.hidden = !view.hidden
                          }
                          UIView.animateWithDuration(0.3, animations: {
                              cell.contentView.layoutIfNeeded()
                              tableView.beginUpdates()
                              tableView.endUpdates()
                  
                          })
                      }
                  }
                  

                  推荐答案

                  似乎需要在 UITableViewCell 的 init 中添加约束并添加到 contentView 而不是单元格的视图.

                  It seems that for this to work the constraints need to be added in the init of the UITableViewCell and added to the contentView instead of cell's view.

                  工作代码如下所示:

                  import UIKit
                  class StackCell : UITableViewCell {
                      enum VisualFormat: String {
                          case HorizontalStackViewFormat = "H:|[stackView]|"
                          case VerticalStackViewFormat = "V:|[stackView(>=44)]|"
                      }
                  
                      var hasSetupConstraints = false
                      lazy var stackView : UIStackView! = {
                          let stack = UIStackView()
                          stack.axis = UILayoutConstraintAxis.Vertical
                          stack.distribution = .FillProportionally
                          stack.alignment = .Fill
                          stack.spacing = 3.0
                          stack.translatesAutoresizingMaskIntoConstraints = false
                          stack.setContentCompressionResistancePriority(UILayoutPriorityRequired, forAxis: .Vertical)
                          return stack
                      }()
                  
                      override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
                          super.init(style: style, reuseIdentifier: reuseIdentifier)
                          contentView.addSubview(stackView)
                          addStackConstraints()
                      }
                  
                      required init?(coder aDecoder: NSCoder) {
                          fatalError("init(coder:) has not been implemented")
                      }
                  
                      private func addStackConstraints() {
                          let viewsDictionary: [String:AnyObject] = ["stackView" : stackView]
                          var newConstraints = [NSLayoutConstraint]()
                          newConstraints += self.newConstraints(VisualFormat.HorizontalStackViewFormat.rawValue, viewsDictionary: viewsDictionary)
                          newConstraints += self.newConstraints(VisualFormat.VerticalStackViewFormat.rawValue, viewsDictionary: viewsDictionary)
                          contentView.addConstraints(newConstraints)
                          super.updateConstraints()
                      }
                  
                      private func newConstraints(visualFormat: String, viewsDictionary: [String:AnyObject]) -> [NSLayoutConstraint] {
                          return NSLayoutConstraint.constraintsWithVisualFormat(visualFormat, options: [], metrics: nil, views: viewsDictionary)
                      }
                  }
                  

                  这篇关于使用 UIStackView 的动态 UITableView 行高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                  • <bdo id='WGrpN'></bdo><ul id='WGrpN'></ul>
                    <i id='WGrpN'><tr id='WGrpN'><dt id='WGrpN'><q id='WGrpN'><span id='WGrpN'><b id='WGrpN'><form id='WGrpN'><ins id='WGrpN'></ins><ul id='WGrpN'></ul><sub id='WGrpN'></sub></form><legend id='WGrpN'></legend><bdo id='WGrpN'><pre id='WGrpN'><center id='WGrpN'></center></pre></bdo></b><th id='WGrpN'></th></span></q></dt></tr></i><div id='WGrpN'><tfoot id='WGrpN'></tfoot><dl id='WGrpN'><fieldset id='WGrpN'></fieldset></dl></div>

                      • <tfoot id='WGrpN'></tfoot>

                            <tbody id='WGrpN'></tbody>
                        1. <small id='WGrpN'></small><noframes id='WGrpN'>

                          <legend id='WGrpN'><style id='WGrpN'><dir id='WGrpN'><q id='WGrpN'></q></dir></style></legend>