UICollectionView分页无法正常使用swift

问题描述

我正在使用UITableView。在UITableViewCell内放置了一个UICollectionView。 我在一节中使用2 UICollectionView行。 当前,两行都一起滚动。 我需要使用分页显示UICollectionView单元格,并希望在左侧显示下一部分单元格的一小部分。

我尝试将尺寸设置为UICollectionView,然后滚动到下一部分。.它将被切断。 我希望下一节的x点值也应从零开始。 我不想使用任何第三方。

我点击了以下链接,但没有成功。

Horizontally scroll ALL rows of UICollectionView together

UICollectionView Horizontal Paging not centered

Paging UICollectionView by cells,not screen

//MARK:- Collection view delegate and datasource
extension MasonryGridViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout  {

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

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

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageDataCollectionViewCell",for: indexPath) as? ImageDataCollectionViewCell
     if (nil == cell) {
         let nib:Array = Bundle.main.loadNibNamed("ImageDataCollectionViewCell",owner: self,options: nil)!
         cell = nib[0] as? ImageDataCollectionViewCell
     }
    cell?.populateData(atItemIndex: indexPath.row)
    return cell ?? UICollectionViewCell()
}

func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
    print(indexPath.item)
}

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
    
    var collectionWidthForItems = collectionView.bounds.width - 8
    
    let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
    let collectionHeightForItems = (collectionView.bounds.size.height - (collectionViewLayout?.minimumInteritemSpacing ?? 2))
    return CGSize(width: collectionWidthForItems / 2,height:collectionHeightForItems / 2)
}

func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView,withVeLocity veLocity: CGPoint,targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    
    if let cell = tableViewData.cellForRow(at: IndexPath(row: 0,section: 0)) as? TableViewGridCelll {
        
        if scrollView == cell.collectionItems {
            let itemWidth = CGFloat((cell.collectionItems.bounds.width) - 8)//cellSize.width + spacing
            let inertialTargetX = targetContentOffset.pointee.x
            let offsetFromPrevIoUsPage = (inertialTargetX + cell.collectionItems.contentInset.left).truncatingRemainder(dividingBy: itemWidth)
            
            // snap to the nearest page
            let pagedX: CGFloat
            if offsetFromPrevIoUsPage > itemWidth / 2 {
                pagedX = inertialTargetX + (itemWidth - offsetFromPrevIoUsPage)
            } else {
                pagedX = inertialTargetX - offsetFromPrevIoUsPage
            }
            
            let point = CGPoint(x: pagedX + 20,y: targetContentOffset.pointee.y)
            targetContentOffset.pointee = point
        }
    }
  }
}

它不起作用。

任何建议都会有所帮助。

我也不想使用任何第三方。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)