在UICollectionview iOS 8中包装单元格

时间:2015-05-09 02:57:38

标签: ios swift uicollectionview

我对this question有同样的问题。我已经尝试了解决方案,但没有调用它。我应该在哪里实现或调用UICollectionViewFlowLayout的方法或子类。

我应该在哪里使用它? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

你可以这样做,这个方法是自动调用的,

swift version

首先创建一个新类,它是UICollectionViewFlowLayout的子类,例如

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

中的

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

在xcode中创建一个新文件,通过继承UICollectionViewFlowLayout,让它的名字为MyCustomCollectionViewFlowLayout,并在MyCustomCollectionViewFlowLayout .m文件中放置代码

#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

和你正在使用集合视图的类只导入MyCustomCollectionViewFlowLayout.h这个并将其设置为集合视图

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