如果从另一个视图控制器调用,则集合视图中的单元格不可见

时间:2017-07-19 23:09:27

标签: ios swift swift3 uicollectionview uicollectionviewcell

以下是ProductViewController的代码

import UIKit

class ProductViewController: UIViewController, UICollectionViewDataSource,   UICollectionViewDelegate {
@IBOutlet weak var myCollectionView: UICollectionView!

let array:[String] = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

override func viewDidLoad() {
    super.viewDidLoad()

    let itemSize = UIScreen.main.bounds.width/3 - 2

    let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: itemSize, height: itemSize)

    layout.minimumInteritemSpacing = 2
    layout.minimumLineSpacing = 2

    myCollectionView.collectionViewLayout = layout
}

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


//Number of views
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array.count
}

//Populate view
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell
    cell.myImageView.image = UIImage(named: array[indexPath.row] + ".JPG")
    return cell
}

}

但我从ViewController代码中的按钮调用此ProductViewController就在这里

 @IBAction func view(_ sender: AnyObject) {
    let LogInVController = self.storyboard?.instantiateViewController(withIdentifier: "ProductViewController") as! ProductViewController
    self.present(LogInVController, animated: true)

}

如果我使用ViewController中的ProductViewController代码制作独立应用程序,我可以看到collectionview的内容单元格。 但是,如果我从ViewController按钮调用ProductViewController,则单元格不可见。 请帮忙,因为我是iOS新手 提前谢谢。

1 个答案:

答案 0 :(得分:1)

是的,谢谢@Hosny我没有连接数据源和委托, 这就是我遇到这个问题的原因。 因为我是新人所以从下次开始,我会交叉检查。 非常感谢。

我在this thread

中找到了如何连接它们的方法