के भीतर UIStackView का खराब प्रदर्शन मैं UIStackView
का उपयोग पर UICollectionViewCell
सबक्लास में कर रहा हूं। मैं आईओएस एसडीके 9.2UIColackViewCells
संग्रह दृश्य की स्क्रॉलिंग चिकनी है यदि मैं लेबल 'text
लेबल को अद्यतन नहीं करता हूं, तो मैं उन्हें हटा देता हूं। हालांकि, अगर मैं उन्हें text
अपडेट करता हूं जैसे कि मैं उन्हें अस्वीकार करता हूं, स्क्रॉलिंग बहुत धीमी है।
मैंने डिवाइस को चलाने के लिए समस्या को दिखाने के लिए बहुत छोटा डेमो बनाया (सिम्युलेटर नहीं)। आप एक नया खाली परियोजना बना सकते हैं और इस के साथ ViewController.swift
की सामग्री को बदलने कर सकते हैं:
import UIKit
class ViewController: UIViewController {
override func loadView() {
view = UIView()
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 100, height: 200)
let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
collectionView.registerClass(Cell.self, forCellWithReuseIdentifier: "Cell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.dataSource = self
view.addSubview(collectionView)
let constraints = ["H:|-[collectionView]-|",
"V:|[collectionView]|"
].flatMap { NSLayoutConstraint.constraintsWithVisualFormat($0, options: [], metrics: nil, views: ["collectionView": collectionView])
}
NSLayoutConstraint.activateConstraints(constraints)
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! Cell
//comment out the line below to make the scrolling smoother:
cell.fillLabels()
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
}
class Cell: UICollectionViewCell {
var labelArray = [UILabel]()
func fillLabels() {
for label in labelArray {
label.text = "\(label.text!) yo"
}
}
override init(frame: CGRect) {
super.init(frame: frame)
contentView.backgroundColor = UIColor.whiteColor()
let stackView = UIStackView()
stackView.axis = .Horizontal
stackView.alignment = .Leading
stackView.distribution = .EqualSpacing
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)
let leftStack = UIStackView()
leftStack.axis = .Vertical
let rightStack = UIStackView()
rightStack.axis = .Vertical
stackView.addArrangedSubview(leftStack)
stackView.addArrangedSubview(rightStack)
for index in 0...10 {
let leftLabel = UILabel()
leftLabel.text = "\(index)"
leftStack.addArrangedSubview(leftLabel)
labelArray.append(leftLabel)
let rightLabel = UILabel()
rightLabel.text = "\(index)"
rightStack.addArrangedSubview(rightLabel)
labelArray.append(rightLabel)
}
let constraints = [
"H:|[stackView]|",
"V:|[stackView]|"
].flatMap {
NSLayoutConstraint.constraintsWithVisualFormat($0, options: [], metrics: nil, views: ["stackView": stackView])
}
NSLayoutConstraint.activateConstraints(constraints)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
आप देखेंगे स्क्रॉल चिकनी है जब आप fillLabels
करने के लिए कॉल बाहर टिप्पणी।
यदि आप UIStackViews
के बिना एक ही लेआउट को पुन: पेश करने का प्रयास करते हैं और fillLabels
पर कॉल शामिल करते हैं, तो आप देखेंगे कि स्क्रॉलिंग भी चिकनी है।
यह सुझाव देता है कि UIStackView
प्रदर्शन लेआउट को पीड़ित करता है यदि यह अपने लेआउट को फिर से समझता है।
क्या यह परिकल्पना सही है? क्या कुछ समाधान हैं?
हाय एरिक! क्या आप इस पर कोई निष्कर्ष निकालने में सक्षम थे? – damirstuhec
नहीं, बग दायर किया गया: https://openradar.appspot.com/24206043 – Eric
क्या आपके पास UICollectionView के लिए विशेषता निरीक्षक में सक्षम प्रीफ़ेचिंग विकल्प है – Balanced