繁体   English   中英

如何设置collectionview swift的单元格位置?

[英]How to set position of cell of collectionview swift?

我正在使用collectionview以全屏显示图像并允许在不同图像之间滚动。 但是当方向改变时,它的显示不正确。 这里的方向改变后的屏幕图像 这是我的代码

override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {


//    collectionView.reloadData()
    collectionView.collectionViewLayout.invalidateLayout()
    if visibleCell != nil {

     collectionView.selectItem(at: visibleCell as IndexPath?, animated: true, scrollPosition: .centeredHorizontally)

     }
}


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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCollectionViewCell
        print(cell.frame)
      cell.SectionImage.image = images[indexPath.row].image

      return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: self.view.frame.width, height: self.view.frame.height)

}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

使用UICollectionView sizeForItemAtIndexPath方法定义单元格的高度和宽度。

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

    let leftAndRightPaddings: CGFloat = 15.0
    let numberOfItemsPerRow: CGFloat = CGFloat(NUMBER_OF_ITEMS_PER_ROW)

    let bounds = UIScreen.mainScreen().bounds
    let width = (bounds.size.width - leftAndRightPaddings * (numberOfItemsPerRow+1)) / numberOfItemsPerRow
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = CGSizeMake(width, width)

    return layout.itemSize
}

试试吧。 你会得到你期望的结果。

首先将单元格和行的最小间距设置为0。

在此输入图像描述

之后,使用委托方法返回等于UICollectionView的单元格大小

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

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    dispatch_async(dispatch_get_main_queue()) {
        self.collectionView.reloadData()
    }
    print_debug("change orientation")
}

它在同样的情况下为我工作。

你可以在你给单元格的大小UICollectionViewUICollectionViewDelegateFlowLayout委托方法sizeForItemAt indexPath(:)

// MARK: - Collection view flow layout delegate methods

extension ViewController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let numberOfCell: CGFloat = 1.0 // Give number of cells you want in your collection view.
    let screenWidth = UIScreen.main.bounds.size.width
    let cellWidth = screenWidth / numberOfCell
    let cellHeight = collectionView.frame.size.height 
    return CGSize(width: cellWidth, height: cellHeight)

}

}

谢谢:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM