2016-07-17 85 views
0

我是Swift Ios編程的新手。
首先,我很抱歉因爲我的英文寫作不好。
我想設置數據源&從另一個Swift類的UICollectionView委託,但我的Swift類函數不調用,我的CollectionView不顯示任何東西。
如果我的數據源&代表設置爲自我一切正常。
我的DataSource類是:
LivePlayListAdapter.swift
Swift UICollectionView not以編程方式設置數據源和委託

import UIKit 

class LivePlayListAdapter:UIViewController, UICollectionViewDataSource,UICollectionViewDelegate { 


    let numberOfCol:CGFloat = 2 
    let reuseIdentifier = "cell" 

    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"] 



    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.items.count 

    } 

    // make a cell for each cell index path 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell 


     cell.labelText = items[indexPath.row] 



     return cell 
    } 

    // MARK: - UICollectionViewDelegate protocol 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     // handle tap events 
     print("You selected cell #\(indexPath.item)!") 
    } 

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

     guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else { 
      return CGSize() 
     } 

     let widthAvailbleForAllItems = (collectionView.frame.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right) 

     let widthForOneItem = widthAvailbleForAllItems/numberOfCol - flowLayout.minimumInteritemSpacing 
     return CGSize(width: CGFloat(widthForOneItem), height: CGFloat(widthForOneItem)) 
    } 
} 


這是我的ViewController類:
LivePlayController.swift 進口的UIKit

class LivePlayController: UIViewController { 

    var heyatPlayGridView : UICollectionView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.heyatPlayGridView = createCollectionView() 
     self.view.addSubview(self.heyatPlayGridView) 
     self.heyatPlayGridView.reloadData() 
     self.heyatPlayGridView.reloadInputViews() 
    } 

func createCollectionView() -> UICollectionView{ 

     let collectionView : UICollectionView! 
     let width = UIScreen.mainScreen().bounds.size.width - 6 
     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5) 
     layout.itemSize = CGSizeMake(width/3, width/3) 
     layout.minimumInteritemSpacing = 3 
     layout.minimumLineSpacing = 3 

     collectionView = UICollectionView(frame: CGRectMake(10, 110, 300, 400), collectionViewLayout: layout) 
     let lid = LivePlayListAdapter() 
     collectionView.dataSource = lid 
     collectionView.delegate = lid 
     collectionView.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: "cell") 
     collectionView.backgroundColor = UIColor.blueColor() 


     return collectionView 
    } 
} 


而我CustomCell類:
MyCollectionViewCell.swift

import UIKit 

class MyCollectionViewCell: UICollectionViewCell { 
    let pictureSize = 90; 
    let marginTop = 20 
    var imageUrl: String = "nature_pic_2" 
    var labelText : String = "ss" 

    override init(frame: CGRect) { 
     var a:CGFloat = CGFloat((frame.width-CGFloat(self.pictureSize))/2) 
     if (a<0){ 
      a=1 
     } 
     let imageView = UIImageView(frame: CGRectMake(a,CGFloat(marginTop), CGFloat(self.pictureSize), CGFloat(self.pictureSize))); 
     let label = UILabel(frame: CGRectMake(0, CGFloat(frame.width-40) , CGFloat(frame.width), CGFloat(30))); 
     super.init(frame: frame) 


      removeView(); 
     // set as you want 
     let image = UIImage(named: imageUrl); 
     imageView.image = image; 
     imageView.layer.cornerRadius = CGFloat(CGFloat(pictureSize)/2.0) 
     imageView.layer.masksToBounds = true 
     imageView.layer.borderColor = UIColor.whiteColor().CGColor 
     imageView.layer.borderWidth = 1 
     imageView.tag = 101 
     self.addSubview(imageView); 


     label.text = labelText; 
     label.textColor = UIColor.whiteColor() 
     label.font = UIFont(name: Fonts.FarsiFont, size: 23) 
     label.textAlignment = NSTextAlignment.Center 
     //  label.backgroundColor = UIColor.purpleColor() 
     label.lineBreakMode = NSLineBreakMode.ByWordWrapping 
     label.numberOfLines = 0 
     label.tag = 100 
     //  label.sizeToFit() 
     self.addSubview(label); 

    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func removeView() { 
     if let viewWithTag = self.viewWithTag(100) { 
      print("added") 
      viewWithTag.removeFromSuperview() 
     }else{ 
      print("No!") 
     } 

     if let viewWithTag = self.viewWithTag(101) { 
      print("added") 
      viewWithTag.removeFromSuperview() 
     }else{ 
      print("No!") 
     } 

    } 
} 

回答

2

爲什麼dataSourcedelegate沒有被設置你正在創建的LivePlayController這就是一個新的實例。

在這裏創造新的實例

let lid = LivePlayListAdapter() 
collectionView.dataSource = lid 
collectionView.delegate = lid 

更改爲

collectionView.dataSource = self 
collectionView.delegate = self 

更新

struct UICreator{ 
    static func createCollectionView() -> UICollectionView{ 

    let collectionView : UICollectionView! 
    let width = UIScreen.mainScreen().bounds.size.width - 6 
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5) 
    layout.itemSize = CGSizeMake(width/3, width/3) 
    layout.minimumInteritemSpacing = 3 
    layout.minimumLineSpacing = 3 

    collectionView = UICollectionView(frame: CGRectMake(10, 110, 300, 400), collectionViewLayout: layout) 
// collectionView.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: "cell") 
    collectionView.backgroundColor = UIColor.blueColor() 

    return collectionView 
    } 
} 


class ExampleVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{ 

    var myCollectionView: UICollectionView! 

    override func viewDidLoad() { 
    myCollectionView = UICreator.createCollectionView() 
    myCollectionView.delegate = self 
    myCollectionView.dataSource = self 

    } 
} 
+0

謝謝你,我的數據源是不是在這個Vie的wController類。請完整閱讀我的代碼並回答我。 –

+0

爲什麼不在它使用的viewcontroller中創建它?或者在一個結構中創建一個靜態func並使用它的返回值? (答案已更新) – kye

相關問題