我对 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/