Ios UICollectionView分页未按预期使用swift
我使用的是Ios UICollectionView分页未按预期使用swift,ios,swift,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我使用的是UITableView。在UITableViewCell中,我放置了一个UICollectionView。 我在一个节中使用2UICollectionView行。 当前两行正在一起滚动。 我需要使用分页显示UICollectionView单元格,并希望在左侧显示下一节单元格的一小部分 我尝试将大小指定给UICollectionView,然后滚动到下一节。它将被切断。 我想下一节x点的值也应该从零开始。 我不想使用任何第三方 我已经按照下面的链接,但没有成功 //标记:-集合视图委
UITableView
。在UITableViewCell
中,我放置了一个UICollectionView
。
我在一个节中使用2UICollectionView
行。
当前两行正在一起滚动。
我需要使用分页显示UICollectionView
单元格,并希望在左侧显示下一节单元格的一小部分
我尝试将大小指定给UICollectionView
,然后滚动到下一节。它将被切断。
我想下一节x点的值也应该从零开始。
我不想使用任何第三方
我已经按照下面的链接,但没有成功
//标记:-集合视图委托和数据源
扩展MasonryGridViewController:UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{
func numberOfSections(在collectionView:UICollectionView中)->Int{
返回1
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回20
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
var cell=collectionView.dequeueReusableCell(带有ReuseIdentifier:“ImageDataCollectionViewCell”,for:indexPath)作为?ImageDataCollectionViewCell
if(nil==单元格){
让nib:Array=Bundle.main.loadNibNamed(“ImageDataCollectionViewCell”,所有者:self,选项:nil)!
cell=nib[0]作为?ImageDataCollectionViewCell
}
单元格?.populateData(atItemIndex:indexPath.row)
返回单元格??UICollectionViewCell()
}
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
打印(indexPath.item)
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
var collectionWidthForItems=collectionView.bounds.width-8
让collectionViewLayout=collectionView.collectionViewLayout为?UICollectionViewFlowLayout
让collectionHeightForItems=(collectionView.bounds.size.height-(collectionViewLayout?.MinimumItemSpacing±2))
返回CGSize(宽度:collectionWidthForItems/2,高度:collectionHeightForItems/2)
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,MinimumLineSpacingforSection:Int)->CGFloat{
返回0
}
func ScrollViewWillendDraging(scrollView:UIScrollView,withVelocity:CGPoint,targetContentOffset:UnsafemeutablePointer){
如果让cell=tableViewData.cellForRow(at:IndexPath(行:0,节:0))作为?TableViewGridCelll{
如果scrollView==cell.collectionItems{
让itemWidth=CGFloat((cell.collectionItems.bounds.width)-8)//cellSize.width+间距
设inertialTargetX=targetContentOffset.pointee.x
let offsetFromPreviousPage=(inertialTargetX+cell.collectionItems.contentInset.left)。截断余数(dividingBy:itemWidth)
//捕捉到最近的页面
让pagedX:CGFloat
如果从上一页>项目宽度偏移/2{
pagedX=惯性目标X+(itemWidth-与上一页的偏移量)
}否则{
pagedX=惯性目标Tx-与上一页的偏移量
}
设point=CGPoint(x:pagedX+20,y:targetContentOffset.pointee.y)
targetContentOffset.pointee=点
}
}
}
}
它不起作用
任何建议都会有帮助
我也不想使用任何第三方
//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, layout collectionViewLayout: UICollectionViewLayout, 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
}
}
}
}