首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionview iOS 8中的包裹细胞

UICollectionview iOS 8中的包裹细胞
EN

Stack Overflow用户
提问于 2015-05-09 02:57:38
回答 1查看 2.3K关注 0票数 0

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

我该在哪儿用这个?提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-05-09 05:44:33

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

快速版

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

代码语言:javascript
复制
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类中

代码语言:javascript
复制
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文件中放置代码

代码语言:javascript
复制
#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,并将其设置为集合视图。

代码语言:javascript
复制
- (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
 }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30135454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档