Ios UICollectionView具有多行

Ios UICollectionView具有多行,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我使用UICollectionView(带有UIButtons的视图)预订时间: override func viewDidLoad() { super.viewDidLoad() collectionViewTime.register(UINib(nibName: "TimeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "timecell")

我使用UICollectionView(带有UIButtons的视图)预订时间:

    override func viewDidLoad() {
        super.viewDidLoad()        
        collectionViewTime.register(UINib(nibName: "TimeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "timecell")
        collectionViewTime.dataSource = self
        collectionViewTime.delegate = self        
        if let layout = collectionViewTime.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .vertical
        }

    }

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

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {            
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        let numberOfItems = CGFloat(collectionView.numberOfItems(inSection: 0))
        let combinedItemWidth = (numberOfItems * flowLayout.itemSize.width) + ((numberOfItems - 1)  * flowLayout.minimumInteritemSpacing)
        let padding = (collectionView.frame.width - combinedItemWidth) / 2
        return UIEdgeInsets(top: 0, left: padding, bottom: 0, right: padding)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {            
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timecell", for: indexPath) as! TimeCollectionViewCell            
        let time = Timeline[indexPath.row][0]
        cell.btnTime.setTitle(DateManager.dateToTime(date: time.0), for: .normal)            
        return cell
    }
我在一行中获得了所有视图(UIButtons): 如何设置新行中的自动移动视图?
因此,在我的例子(iPad)中,一行将包含7个UIView,iPhone将在一行中包含4-5个UIView

为此,您需要实现
UICollectionViewDelegateFlowLayout
方法
collectionView(uuu∶layout:sizeForItemAt:)
,并将
collectionView
滚动方向更改为
垂直

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let minCellSpace: CGFloat = 10 //Set minimum cell space that you want
    if UIDevice.current.userInterfaceIdiom == .pad {
         //For iPad return 7 cell as you mention in question
         return CGSize(width: (collectionView.frame.size.width / 7) - minCellSpace, height: yourCellHeight)
    }
    else {
         //For iPhone return 4 cell, if you want to return 5 cell divide width to 5
         return CGSize(width: (collectionView.frame.size.width / 4) - minCellSpace, height: yourCellHeight)
    }
}

请使用一个以上的流程委托方法,如下所示

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize;
------
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:(button width), height:button hieght);
    }

UICollectionView计算它的宽度和单元格宽度时要记住偏移量,所以如果UICollectionView的宽度是320,一个单元格是200,它将在另一个单元格下面显示一个单元格,所以您需要按照希望它显示单元格的方式调整宽度。如果要并排显示,在这种情况下,两个单元格的宽度应与UICollectionViewI的总宽度相适应。我添加了此方法,它调用了,但视图不会在新的单元格上移动rows@Kirill您需要将collectionView滚动方向设置为垂直而非水平。请参阅上面的代码。我写了layout.scrollDirection=。vertical@Kirill如果让
阻塞将方向设置为垂直的位置,则将打印语句放入
,并检查它是否正在控制台中打印。如果它在控制台中打印,那么您需要增加
collectionview
高度,因为如果您滚动collectionview,它将在新行中显示单元格。我选中了,它在控制台中打印。我添加了collectionViewTime.frame.size.height=300(我的单元格高度=45),但它不起作用