Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
集合视图网格iOS Swift 2_Ios_Swift_Grid_Uicollectionview_Uicollectionviewlayout - Fatal编程技术网

集合视图网格iOS Swift 2

集合视图网格iOS Swift 2,ios,swift,grid,uicollectionview,uicollectionviewlayout,Ios,Swift,Grid,Uicollectionview,Uicollectionviewlayout,因此,我有一个工作集合视图,其中有2列,每列中有3项。网格视图工作得很好,直到我进入iphone5s模拟器,然后我的网格视图变成一列而不是两列。我读过很多关于自定义布局的帖子,但似乎都不管用。感谢您的帮助。这是我的密码: override func viewDidLoad() { super.viewDidLoad() let screenWidth = UIScreen.mainScreen().bounds.size.width let screenHeight =

因此,我有一个工作集合视图,其中有2列,每列中有3项。网格视图工作得很好,直到我进入iphone5s模拟器,然后我的网格视图变成一列而不是两列。我读过很多关于自定义布局的帖子,但似乎都不管用。感谢您的帮助。这是我的密码:

 override func viewDidLoad() {
    super.viewDidLoad()
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height


    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
    layout.itemSize = CGSize(width: (screenWidth - 32)/2, height: 175)

    var collview = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight), collectionViewLayout: layout)


    [collview.reloadData];


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

   let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell

    let myImage = UIImage(named: pictureArray[indexPath.row])

    cell.textView.text = textArray[indexPath.row]

    cell.imageView.image = myImage


    return cell
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return pictureArray.count
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSize(width: 175, height: 175)
}

因为您将每个项目的大小设置为
CGSize(宽:175,高:175)
,所以当您更换其他手机时,它将无法正常工作

只需这样更改:(将
UICollectionViewDelegate
更改为
UICollectionViewDelegateFlowLayout


我收到一个错误,上面写着“UIView的值类型没有成员大小”,我刚刚更新了答案,请再次检查。您可以使用collectionView.size.frame.width/3wird,现在是说UICollectionView的值没有成员大小。我的代码有没有做错什么?所以,你可以使用screenWidth/3awesome,这是可行的,但现在我的单元格大小比应该的小。我希望单元格的高度和宽度为175,我认为默认值为50。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSize(width: screenWidth / 3, height: screenWidth / 3)
}