Reputation: 726
Situation:
I use the UITableViewController
with static table cells designed in Storyboard. In my first UITableViewCell
I implemented a nice UICollectionView
.
However there is one problem: The UICollectionViewCell
moves out of the frame when I scroll through them. They should stop only in the right positions.
Code:
import UIKit
class HomeTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var topSliderCollectionView: UICollectionView!
var topSliderElement = [TopSliderElement]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return topSliderElement.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "slider cell", for: indexPath) as! HomeSliderCollectionViewCell
let slider = topSliderElement[indexPath.row]
cell.titleLbl.text = slider.tile
cell.descriptionLBL.text = slider.description
return cell
}
}
I tried this:
This is a possible solution, but it's not working because I don't use a normal UIViewController
.
Question: How can I control the cells so that they stop at the right position in the view?
Upvotes: 0
Views: 702
Reputation: 1598
You will have to set your cells width to be the same with as your view.frame.width
for this to work properly. If you have any spacing on the sides of your cell (which you do), you'll have to add those together and then minus that from the width. I.e, view.frame.width - 16
. Once you've done this, you'll have to set isPagingEnabled
to true in your UICollecitonView
- this will lock the cell to the centre of the screen when you stop scrolling.
Upvotes: 2