Apple Ramos
Apple Ramos

Reputation: 275

Wrapping cells in UICollectionview iOS 8

I have the same problem with this question. I already tried the solution but it is not called. Where should I implement or call the method or subclass of UICollectionViewFlowLayout.

Where should I use this? Thanks in advance.

Upvotes: 0

Views: 2292

Answers (1)

Shankar BS
Shankar BS

Reputation: 8402

U can do like below, this method is called automatically,

swift version

first create a new class which is the subclass of UICollectionViewFlowLayout for example

import UIKit

class CustomLayout: UICollectionViewFlowLayout
{

override init() {
    super.init()
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

var newAttributes:[AnyObject] = []
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
    super.layoutAttributesForElementsInRect(rect)
    var attributes:[AnyObject] = super.layoutAttributesForElementsInRect(rect)!
    //arrayWithCapacity(attributes.count)
    //configure your attributes for each item hear and store it in separate array and return that array in below example i am sending the same attributes.

    return attributes
   }
}

in ViewController class

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var aCollectionView: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    var customLayout:CustomLayout = CustomLayout() //initilise the custom layout for collection view

    customLayout.minimumLineSpacing      = 0.33 //set the offset between items
    customLayout.minimumInteritemSpacing = 0.0
    customLayout.itemSize = CGSizeMake(50.0, 50.0)
    aCollectionView.collectionViewLayout = customLayout //set it to collection view
    var cellNib:UINib = UINib(nibName: "CollectionViewCell", bundle: nil)
    aCollectionView.registerNib(cellNib, forCellWithReuseIdentifier: "CELL")
}

objective-c version

in xcode crate a new file by subclassing the UICollectionViewFlowLayout lets say it's name as MyCustomCollectionViewFlowLayout and in MyCustomCollectionViewFlowLayout .m file place the code

#import "MyCustomCollectionViewFlowLayout.h"

@implementation MyCustomCollectionViewFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
   [super layoutAttributesForElementsInRect:rect];
   NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
   NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
  for (UICollectionViewLayoutAttributes *attribute in attributes)
   {
      if ((attribute.frame.origin.x + attribute.frame.size.width <= ceil(self.collectionViewContentSize.width)) &&
        (attribute.frame.origin.y + attribute.frame.size.height <= ceil(self.collectionViewContentSize.height)))
      {
          [newAttributes addObject:attribute];
      }
  }
  return newAttributes;
}

- (void)dealloc
{
   [super dealloc];
}

@end

and the class where u are using the collection view just import MyCustomCollectionViewFlowLayout.h this and set it to collection view

- (void)viewDidLoad
 {
   [super viewDidLoad];
    //....other codes 

   MyCustomCollectionViewFlowLayout *flowLayout = [[MyCustomCollectionViewFlowLayout alloc]init];

  [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  //set according to your settings
  flowLayout.minimumInteritemSpacing = 0.0f;
  flowLayout.minimumLineSpacing      = 0.33f; //set the offset between items
  _collectionView.pagingEnabled = YES;
  _collectionView.bounces       = NO;
  _collectionView.showsHorizontalScrollIndicator = NO;
  _collectionView.showsVerticalScrollIndicator   = NO;
  [_collectionView setCollectionViewLayout:flowLayout]; //set your custom flow layout hear
  [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier]; //set the custom cell
 }

Upvotes: 2

Related Questions