2015-02-20 12 views
7

iOS geliştirmede yepyeni oluyorum ve Android'de iOS'a sahip olduğum bir projeyi uygulamaya çalışıyorum. Uygulamanın bir parçası olarak kodda bir koleksiyonView oluşturmaya çalışıyorum. Şey, her hücre içindeki koleksiyon görünümündeki her bir hücreden farklı bir boyutta olabilmesidir. , komşusuna farklı bir boyutta olabilir. Ben ekli kodu deniyorum ama bir iPhone 6 ile ya fiziksel bir cihaz ya da simülatörde UI beklendiği gibi görünüyor fark ettim .. başka bir şey korkunç kırık görünüyor. Onu kırıyor sizeForIndexItemAtPath gibi görünüyor. Herhangi bir fikrin var mı? TIAiOS collectionView.sizeForItemAtIndexPath, iPhone 6'dan Önce Her Şey Üzerinde Breaks Oluyor

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

Gördüğünüz şeyleri (örneğin, ekran görüntüleri) ne göreceğinize (örneğin, ekran görüntülerini) açıklayabilir misiniz? – Alex

+0

Peki, buna inanır mısın ... Dün gece geç saatlerde çalışıyordum ve gününden vazgeçtim. Ben sadece ekran görüntülerini almak için Xcode açtım ve kod şimdi çalışıyor ... –

+0

Ben uyurken ben onu tamir ettim. Xcode'u açmanızı isteyen bu yorumu yazdım. – Alex

cevap

9

Sen olmak düzenine öğe boyutunu kuruyorsun:

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

Ve ayrıca tarafından geçersiz ediliyor

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

ilk temsilci yöntemini kullanıyorsanız ikinci. sizeForItemAtIndexPath delege yöntemi geçersiz kılmıyorsa, layout.itemSize her öğe için varsayılan boyuttur. Tamamen farklı görünen bir şeyle layout.itemSize'u geçersiz kılıyorsunuz. Delege işlevini kaldırmayı deneyin sizeForItemAtIndexPath, bu beklediğiniz şeyi yapıyor mu?

İlgili konular