0

I am using a UITableView. Inside UITableViewCell I have placed a UICollectionView. I am using 2 UICollectionView rows in a section. Currently both the rows are scrolling together. I have a requirement to display UICollectionView cells using pagination and want to display the small portion of the next section cell at the left side.

I tried by giving size to UICollectionView and after scroll to the next section..it will be cut off. I want next section x point value also should start from the zero. I don't want to use Any third party.

I have followed the below link but no success.

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, 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
        }
    }
  }
}

It is not working.

Any suggestion will be helpful.

I don't want want to use any Third party as well.

kishor0011
  • 928
  • 1
  • 19
  • 45

0 Answers0