ios - 在 UICollectionview iOS 8 中包装单元格

标签 ios swift uicollectionview

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

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

最佳答案

你可以像下面那样做,这个方法会自动调用,

快速版本

首先创建一个新类,例如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 版本

在 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 View 的类只需导入 MyCustomCollectionViewFlowLayout.h 并将其设置为 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
 }

关于ios - 在 UICollectionview iOS 8 中包装单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30135454/

相关文章:

ios - 在 UITableView 后面设置渐变

ios - 如何实现 UICollectionView 中页面之间的间距?

ios - 从 View 中执行 Segue

ios - 尝试为 Collection View 循环滚动设置 numberOfItemsInSection 时 Int max 崩溃

iOS 核心数据 iCloud 同步 - 设为可选

ios - 使用 GCD 调用时,UIWebView stringByEvaluatingJavaScriptFromString 在 iOS5.0/5.1 上挂起

ios - 如何从另一个变量构建一个变量

ios - 为什么 iPhone 6 及更高版本 wkwebview 不接受底部点击?

ios - 使用源类型相机控制 UIImagePickerController 中的修剪搜索栏

html - 如何在 Webview swift 4 中显示来自 HTML URL 的图像