मैं इस धारणा के तहत था कि यूआईसीओलेक्शन व्यू में ऑटो साइजिंग सेल आईओएस 8 में बहुत आसान हो गया था। तो, शायद मुझे यहां कुछ याद आ रही है।यूआईसीओलेक्शन व्यू सेल्फ साइजिंग कस्टम सेल
मैं अपने लेआउट के रूप में UICollectionViewFlowLayout
का एक उपवर्ग का उपयोग करें:
class BuildCollectionViewFlowLayout: UICollectionViewFlowLayout {
required init(coder: NSCoder) {
super.init(coder: coder)
self.minimumLineSpacing = 1
self.sectionInset.top = 20
self.estimatedItemSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: 90)
}
}
तब मेरे ViewController
जो है UICollectionViewController
का एक उपवर्ग इस तरह दिखता है:
class ViewController: UICollectionViewController {
let CellIdentifier = "CellIdentifier"
let apiClient: APIClient()
var builds:Array<JSON>? = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.collectionView.registerClass(BuildProjectStatusCollectionViewCell.self, forCellWithReuseIdentifier: CellIdentifier)
self.apiClient.getProjects({ (projects, error) -> Void in
if (error == nil) {
dispatch_async(dispatch_get_main_queue(), {
self.builds = projects
self.collectionView.reloadData()
})
}
else {
}
})
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.builds!.count;
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell:BuildProjectStatusCollectionViewCell = self.collectionView.dequeueReusableCellWithReuseIdentifier(CellIdentifier, forIndexPath: indexPath) as BuildProjectStatusCollectionViewCell;
cell.setup(self.builds?[indexPath.row])
return cell;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
और अंत में मेरे विचार सेल:
class BuildProjectStatusCollectionViewCell : UICollectionViewCell {
var projectNameLabel: UILabel!
var lastCommitMessageLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}
override func awakeFromNib() {
self.initialize()
}
func initialize() {
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
self.projectNameLabel = UILabel(forAutoLayout:())
self.lastCommitMessageLabel = UILabel(forAutoLayout:())
self.lastCommitMessageLabel.autoresizingMask = UIViewAutoresizing.FlexibleHeight
self.lastCommitMessageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
self.lastCommitMessageLabel.numberOfLines = 0
self.contentView.addSubview(self.projectNameLabel)
self.contentView.addSubview(self.lastCommitMessageLabel)
setNeedsUpdateConstraints()
}
func setup(project:JSON!) -> Void {
self.projectNameLabel!.text = project["name"].stringValue
let builds: Array<JSON> = project["builds"].arrayValue!
if builds.count == 1 {
if builds[0]["status"].stringValue == "success" {
self.backgroundColor = UIColor(red: 68/255, green: 175/255, blue: 105/255, alpha: 1.0)
}
else {
self.backgroundColor = UIColor(red: 254/255, green: 57/255, blue: 48/255, alpha: 1.0)
}
self.lastCommitMessageLabel!.text = builds[0]["message"].stringValue
}
}
override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes! {
let attr: UICollectionViewLayoutAttributes = layoutAttributes.copy() as UICollectionViewLayoutAttributes
// Without this, it crashes. AutoLayout constraints playing around. Error: the item width must be less than the width of the UICollectionView minus the section insets left and right values.
return attr;
}
override func updateConstraints() {
super.updateConstraints()
self.projectNameLabel.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5), excludingEdge: ALEdge.Bottom)
self.lastCommitMessageLabel.autoPinEdge(ALEdge.Top, toEdge: ALEdge.Bottom, ofView: self.projectNameLabel, withOffset: 10)
self.lastCommitMessageLabel.autoPinEdgeToSuperviewEdge(ALEdge.Leading, withInset: 5)
self.lastCommitMessageLabel.autoPinEdgeToSuperviewEdge(ALEdge.Trailing, withInset: 25)
}
}
कोशिकाओं को प्राप्त होता है आकार लेआउट वर्ग में estimatedItemSize
के रूप में सेट करें।
क्या मुझे preferredLayoutAttributesFittingAttributes
विधि में आइटम ऊंचाई की मैन्युअल गणना करना है?
यदि ऐसा है, तो टेबल दृश्य 100% स्वयं आकार देने वाली पंक्तियों का समर्थन नहीं करते हैं? (http://www.appcoda.com/self-sizing-cells/)
मैं पूर्ण चौड़ाई निर्धारित करने की कोशिश कर रहा हूं, और लचीला ऊंचाई है। 'अनुमानित इटैम आकार 'को केवल 20 तक सेट करना अभी भी टूट जाता है। लेआउट कार्यों पर 'itemSize' सेट करना, यहां तक कि संग्रहदृश्य के समान चौड़ाई के साथ भी। हालांकि 'itemSize' सेट करना', अजीब दुष्प्रभाव है कि लेबल शब्द रैपिंग को रोकते हैं, भले ही 'lineBreakMode = NSLineBreakMode.ByWordWrapping'। – MartinHN
उसने कहा, मुझे लगता है कि मैं संग्रह दृश्य का उपयोग कर रहा हूं जहां एक टेबल दृश्य बेहतर फिट होगा? संग्रह संग्रह का मुद्दा यह नहीं है कि यह लचीला है और इसलिए अधिक निश्चित लेआउट के लिए उपयुक्त नहीं है? – MartinHN
मैं संभव होने पर संग्रह दृश्यों का उपयोग करने का भी प्रयास करता हूं, क्योंकि मुझे लगता है कि टेबल दृश्य वास्तव में सार्वभौमिक यूआई के लिए नहीं हैं। पुन। आपकी पहली टिप्पणी, केवल 'अनुमानित IememSize' लचीला ऊंचाई की अनुमति देता है, इसलिए ऐसा लगता है कि आपके लेबल शब्द-रैपिंग को रोकते हैं, जब वे संभवतः क्लिप हो रहे होते हैं; हालांकि मुझे पूरी तरह से यकीन नहीं है कि एसडीके उन संघर्षों को कैसे संभालता है। कुल मिलाकर, मुझे लगता है कि 'अनुमानित IememSize' वांछित होने के लिए बहुत कुछ छोड़ देता है। – hlfcoding