私はUICollectionViewCell
のサブクラスにUILabels
のレイアウトを使用しています。私はiOS SDKを使用しています9.2UICollectionViewCells内のUIStackViewのパフォーマンスが悪い
デキュー時にラベル '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