2016-03-26 7 views
17

आश्चर्यचकित यह बॉक्स से बाहर काम नहीं कर रहा है, क्योंकि यह स्टैक दृश्यों के लिए एक महत्वपूर्ण उपयोग मामला प्रतीत होता है। मेरे पास UITableViewCell सबक्लास है जो सामग्री दृश्य में UIStackView जोड़ता है। मैं tableView(_cellForRowAtIndexPath:) में स्टैक व्यू में लेबल जोड़ रहा हूं और तालिकादृश्य गतिशील पंक्ति ऊंचाई का उपयोग करने के लिए सेट है, लेकिन यह कम से कम Xcode 7.3 में काम नहीं करता है। मैं इस धारणा के तहत भी था कि एक स्टैक व्यू में व्यवस्थित सबव्यूज़ छुपाएं एनिमेटेबल था, लेकिन यह भी टूटा हुआ लगता है।UIStackView का उपयोग कर गतिशील UITableView पंक्ति ऊंचाई?

इस पर सही तरीके से काम करने के तरीके पर कोई विचार है?

Broken dynamic row heights

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() 

     }) 
    } 
} 

उत्तर

16

ऐसा लगता है कि यह काम करने के लिए बाधाओं UITableViewCell की init में जोड़ा गया है और contentView बजाय सेल की दृष्टि में जोड़े जाने की जरूरत है।

enter image description here

काम कर कोड इस तरह दिखता है:

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) 
    } 
} 
+3

हर बार जब मैं एक सवाल मैं कहाँ UIStackViews प्रोग्राम के रूप में लिख रहा हूँ पोस्ट, हर कोई मुझ पर नफरत करता है। तो मुझे किसी और को यह देखने में खुशी हुई। उत्तर के लिए भी धन्यवाद;) – Trip

+1

@Trip +1 प्रोग्रामेटिक रूप से – netigger

+0

के लिए मुझे टेबल पर 'अनुमानित रोहेइट' सेट करना पड़ा और फिर यह जादुई रूप से काम करना शुरू कर दिया! – Inti

संबंधित मुद्दे