UICollectionViewFlowLayout

UICollectionViewFlowLayout

我对this question也有同样的问题。我已经试过这个解决方案了,但它没有被调用。我应该在哪里实现或调用UICollectionViewFlowLayout的方法或子类。
我应该在哪里用这个?
提前谢谢。

最佳答案

你可以这样做,这个方法是自动调用的,
swift版本
首先创建一个新类,例如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")
}

目标c版本
在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

而使用collection视图的类只需导入MyCustomCollectionViewFlowLayout.hthis并将其设置为collection视图
- (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
 }

10-06 02:56