I have a vertically scrolling UICollectionview with different elements. In one section, I would like to show multiple horizontally scrollable elements. This is how my setup looks like:
A Prototype Cell (subclass of UICollectionViewCell):
class SlidingCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(130, 100)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCellWithReuseIdentifier("SwipeContainedCell", forIndexPath: indexPath) as! UICollectionViewCell
}
}
Whatever I do, it just doesn't scroll horizontally :-(. What am I doing wrong?
make sure that you don't have the nested CollectionView inside an UIView
Related
Also i need to have circular scrolling enabled for that collection view.
Can anyone please help me out?
First of all drag collection view in your respective ViewController and then select scroll direction as Horizontal in activity inspector.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return collectionView.frame.size.width/2
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake((collectionView.frame.size.height) , (collectionView.frame.size.height))
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return img_Arr.count
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell3", forIndexPath: indexPath) as! ImageCaptureCell
cell.CapturedImage.clipsToBounds = true
cell.contentView.frame = cell.bounds
let capt = UIImage(data:img_Arr[indexPath.row] as! NSData,scale:1.0)
cell.CapturedImage.image = capt
return cell
}
// MARK: - UICollectionViewDelegate protocol
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath)
{
let cell = collectionView.cellForItemAtIndexPath(indexPath)
//cell?.backgroundColor = UIColor.redColor()
}
// change background color back when user releases touch
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
// cell?.backgroundColor = UIColor.yellowColor()
}
Additionaly give search bar on top and connet to the table delegate
I have an UIViewController, which contains UIScrollView, which contains a few elements, including UICollectionView. UICollectionView is used to display N buttons in fixed frame. UICollectionView scrolling is disabled. Still, my UIScrollView scrolls not as smoothly as it scrolls without UICollectionView. What my be the problem? Does it have something to do with reusable cells?
code:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("tagsCell", forIndexPath: indexPath) as! TagsCollectionViewCell
cell.backgroundColor = UIColor.clearColor()
cell.tagsButton.setTitle("Test", forState: .Normal)
cell.tagsButton.sizeToFit()
return cell
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!,
sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize
{
let element = "Test" as NSString
let stringSize = element.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(17.0)])
return CGSize(width: stringSize.width + 25, height: 29)
}
If the frame of the scrollView and collectionView is same then it must be a problem to differentiate, which one to call. May be that is why it's creating an issue with the collectionView.
I want to put collection view inside tableview cell. Every tableview cell contains 2 collectionviewCell. Let say I have 8 pic in my data model, I want the collection view looks like the image. How to do this?
Instead Of Using Collection View inside tableview , Use only collection view with vertical scroll and in the method
#pragma mark ------------Collection View Delegates-----------------
override func numberOfSectionsInCollectionView(collectionView:
UICollectionView!) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView!,
numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(collectionView: UICollectionView!,
cellForItemAtIndexPath indexPath: NSIndexPath!) ->
UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as !MyCollectionViewCell
// Configure the cell
let image = UIImage(named: Images[indexPath.row])
cell.imageView.image = image
return cell
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(_collectionViewToAddImages.frame.size.width/2, _collectionViewToAddImages.frame.size.height/2);
}
In this way you will have view like this, just add the pading and you will get the results
I am creating an application in which i want to use UICollectionView. I have used this, but i am seeing the black space between to items and two items are moved to left and right side of screen.
internal func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 20
}
internal func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("productCell", forIndexPath: indexPath) as! ProductCell
return cell
}
internal func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
return 1
}
I have a UICollectionView in my storyboard which has a UICollectionViewCell of which I have clicked and dragged to make its size 100w 100h. I also have a swift file controlling this. The code is as follows:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 4
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 10
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
// Configure the cell
return cell
}
When I run this however, the first row is stretched like this:
How to I get them all to be the same size?
Your issue is not a "stretched" first row cell. It is a section inset of 0 points, which causes the last cell of section 0 to be flush up against the first cell of section 1.
You can fix this issue by implementing UICollectionViewDelegateFlowLayout and setting the section inset. Make your class conform to UICollectionViewDelegateFlowLayout, set collectionView.delegate to self, and implement the desired functions of UICollectionViewDelegateFlowLayout. Relevant delegate functions include:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat