2015-02-20 14 views
7

से पहले कुछ भी तोड़ता है मैं आईओएस विकास के लिए नया ब्रांड हूं और मैं आईओएस पर एंड्रॉइड पर एक परियोजना पोर्ट करने की कोशिश कर रहा हूं। मैं कार्यान्वयन के हिस्से के रूप में कोड में संग्रह दृश्य बनाने की कोशिश कर रहा हूं। बात यह है कि संग्रह दृश्य में प्रत्येक सेल में डेटा अलग-अलग स्ट्रिंग आकार हो सकता है प्रत्येक सेल अपने पड़ोसी के लिए एक अलग आकार हो सकता है। मैं संलग्न कोड के साथ प्रयोग कर रहा हूं लेकिन मैंने देखा है कि एक आईफोन 6 या तो एक भौतिक डिवाइस या सिम्युलेटर में यूआई अपेक्षित दिखता है .. और कुछ और यह बहुत टूटा हुआ दिखता है। ऐसा लगता है कि आकार फॉरइंडेक्सइटम एपपाथ है जो इसे तोड़ रहा है। कोई विचार किसी को भी? TIAआईओएस संग्रह View.sizeForItemAtIndexPath आईफोन 6

import UIKit 

    class ViewController: UIViewController,  UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

    var collectionView: UICollectionView? 
    var thisCellHeight: CGFloat = 0.0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     let screenSize: CGRect = UIScreen.mainScreen().bounds 
     let screenWidth = screenSize.width 
     let screenHeight = screenSize.height 

     var thisWidth = screenWidth - 10 

     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.sectionInset = UIEdgeInsets(top: 70, left: 10, bottom: 10, right: 10) 
     layout.itemSize = CGSize(width: thisWidth, height: 120) 
     collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
     //collectionView!.backgroundView?.backgroundColor = UIColor.blueColor() 
     collectionView!.dataSource = self 
     collectionView!.delegate = self 
     collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
     collectionView!.backgroundColor = hexStringToUIColor("#15ADFF") 
     self.view.addSubview(collectionView!) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 14 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 



     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell 
     cell.backgroundColor = UIColor.whiteColor() 

     var typeLabel = UILabel(frame: CGRectMake(10, 0, 200, 21)) 
     typeLabel.text = "Card Type" 
     typeLabel.font = typeLabel.font.fontWithSize(20) 
     cell.addSubview(typeLabel) 

     var titleLabel = UILabel(frame: CGRectMake(10, 25, 300, 21)) 

     titleLabel.text = "This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title." 
     titleLabel.font = titleLabel.font.fontWithSize(20) 
     titleLabel.numberOfLines = 0 
     titleLabel.sizeToFit() 
     self.thisCellHeight = CGFloat(titleLabel.frame.height) 
     println("The title text is \(self.thisCellHeight) high") 
     cell.addSubview(titleLabel) 

     var authorLabel = UILabel(frame: CGRectMake(10, thisCellHeight + 30, 200, 21)) 
     authorLabel.text = "This Is The Article Author" 
     authorLabel.font = authorLabel.font.fontWithSize(15) 
     cell.addSubview(authorLabel) 

     var timestampLabel = UILabel(frame: CGRectMake(10, thisCellHeight + 50, 200, 21)) 
     timestampLabel.text = "This Is The Timestamp" 
     timestampLabel.font = timestampLabel.font.fontWithSize(15) 
     cell.addSubview(timestampLabel) 

     return cell 
    } 

    func hexStringToUIColor (hex:String) -> UIColor { 
     var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString 

     if (cString.hasPrefix("#")) { 
      cString = cString.substringFromIndex(advance(cString.startIndex, 1)) 
     } 

     if (countElements(cString) != 6) { 
      return UIColor.grayColor() 
     } 

     var rgbValue:UInt32 = 0 
     NSScanner(string: cString).scanHexInt(&rgbValue) 

     return UIColor(
      red: CGFloat((rgbValue & 0xFF0000) >> 16)/255.0, 
      green: CGFloat((rgbValue & 0x00FF00) >> 8)/255.0, 
      blue: CGFloat(rgbValue & 0x0000FF)/255.0, 
      alpha: CGFloat(1.0) 
     ) 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 


     return CGSizeMake(320, 350) 


    } 



} 
+0

क्या आप जो देख रहे हैं उसके बारे में आप क्या उम्मीद कर सकते हैं (शायद स्क्रीनशॉट) क्या आप देख सकते हैं? – Alex

+0

अच्छा आप इस पर विश्वास करेंगे ... मैं कल रात देर रात काम कर रहा था और दिन के लिए छोड़ दिया। मैंने स्क्रीनशॉट लेने के लिए अभी एक्सकोड खोला है और कोड अब काम कर रहा है ... –

+0

जब आप सो रहे थे तो मैंने इसे ठीक कर दिया। मैंने आपको उस कोड को लिखा जो आपको एक्सकोड खोलने के लिए प्रेरित करता है। – Alex

उत्तर

9

आप लेआउट पर आइटम आकार स्थापित कर रहे हैं होने के लिए:

layout.itemSize = CGSize(width: thisWidth, height: 120) 

और तुम भी प्रतिनिधि विधि

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(320, 350) 
} 

पहले द्वारा ओवरराइड किया जा रहा है का उपयोग कर रहे दूसरा। layout.itemSize प्रत्येक आइटम के लिए डिफ़ॉल्ट आकार है, यदि प्रतिनिधि विधि sizeForItemAtIndexPath ओवरराइड नहीं है। आप layout.itemSize को ओवरराइड कर रहे हैं जो कुछ अलग दिखता है। केवल प्रतिनिधि समारोह sizeForItemAtIndexPath को हटाने का प्रयास करें, क्या आप ऐसा करते हैं जो आप करते हैं?