2014-10-25 9 views
5

मैं इस धारणा के तहत था कि यूआईसीओलेक्शन व्यू में ऑटो साइजिंग सेल आईओएस 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/)

उत्तर

4

आपको preferredLayoutAttributesFittingAttributes ओवरराइड करने की आवश्यकता नहीं है। और आपका ओवरराइड सुपर कॉल नहीं करता है, जो दस्तावेज़ों के अनुसार है:

इस विधि का डिफ़ॉल्ट कार्यान्वयन आकार मानों को समायोजित करता है ताकि स्वयं आकार वाले सेल द्वारा किए गए परिवर्तनों को समायोजित किया जा सके। सबक्लास इस विधि को ओवरराइड कर सकते हैं और अन्य लेआउट विशेषताओं को समायोजित करने के लिए इसका उपयोग भी कर सकते हैं। यदि आप इस विधि को ओवरराइड करते हैं और सेल आकार समायोजन चाहते हैं, तो पहले सुपर को कॉल करें और लौटाए गए गुणों में अपना स्वयं का संशोधन करें।

ताकि आपको जो चेतावनी मिलती है वह वैध है। यह आपको बता रहा है कि आपका सेल आपके संग्रह दृश्य से अधिक व्यापक नहीं हो सकता है। और शायद यह है क्योंकि आपने estimatedItemSize सेट करते समय इंटर-आइटम स्पेसिंग (जो डिफ़ॉल्ट रूप से> 0 है) के लिए खाता नहीं था। बहुत छोटा आकार सेट करने का प्रयास करें। यह काम करना चाहिए।

और यदि आप जो कर रहे हैं वह ऑटो-साइज को केवल एक आयाम (यानी हमेशा पूर्ण चौड़ाई रखें) करने का प्रयास कर रहा है, तो परेशान न करें। यह काम नहीं करेगा, क्योंकि estimatedItemSize दोनों आयामों के आकार बदलने के लिए है।

+0

मैं पूर्ण चौड़ाई निर्धारित करने की कोशिश कर रहा हूं, और लचीला ऊंचाई है। 'अनुमानित इटैम आकार 'को केवल 20 तक सेट करना अभी भी टूट जाता है। लेआउट कार्यों पर 'itemSize' सेट करना, यहां तक ​​कि संग्रहदृश्य के समान चौड़ाई के साथ भी। हालांकि 'itemSize' सेट करना', अजीब दुष्प्रभाव है कि लेबल शब्द रैपिंग को रोकते हैं, भले ही 'lineBreakMode = NSLineBreakMode.ByWordWrapping'। – MartinHN

+0

उसने कहा, मुझे लगता है कि मैं संग्रह दृश्य का उपयोग कर रहा हूं जहां एक टेबल दृश्य बेहतर फिट होगा? संग्रह संग्रह का मुद्दा यह नहीं है कि यह लचीला है और इसलिए अधिक निश्चित लेआउट के लिए उपयुक्त नहीं है? – MartinHN

+0

मैं संभव होने पर संग्रह दृश्यों का उपयोग करने का भी प्रयास करता हूं, क्योंकि मुझे लगता है कि टेबल दृश्य वास्तव में सार्वभौमिक यूआई के लिए नहीं हैं। पुन। आपकी पहली टिप्पणी, केवल 'अनुमानित IememSize' लचीला ऊंचाई की अनुमति देता है, इसलिए ऐसा लगता है कि आपके लेबल शब्द-रैपिंग को रोकते हैं, जब वे संभवतः क्लिप हो रहे होते हैं; हालांकि मुझे पूरी तरह से यकीन नहीं है कि एसडीके उन संघर्षों को कैसे संभालता है। कुल मिलाकर, मुझे लगता है कि 'अनुमानित IememSize' वांछित होने के लिए बहुत कुछ छोड़ देता है। – hlfcoding

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