ios - 自定义 UICollectionViewLayout 不同的单元格大小
<p><p>我想像这样自定义 <code>UICollectionViewLayout</code>。
<a href="http://i.stack.imgur.com/aWUIa.jpg" rel="noreferrer noopener nofollow">http://i.stack.imgur.com/aWUIa.jpg</a> </p>
<p>当我滚动第二页图像大小不适合单元格大小时;
<a href="http://i.stack.imgur.com/GOUmF.jpg" rel="noreferrer noopener nofollow">http://i.stack.imgur.com/GOUmF.jpg</a> </p>
<p>然后再次返回第一页;<br/>
<a href="http://i.stack.imgur.com/lQQ3h.png" rel="noreferrer noopener nofollow">http://i.stack.imgur.com/lQQ3h.png</a> </p>
<p>图像大小和单元格大小不匹配。
这是我的自定义布局代码;</p>
<pre><code>//
//SeyahatLayout.m
//SeyahatTrend
//
//Created by Mehmet Can Yavuz on 03.06.2013.
//Copyright (c) 2013 Mehmet Can Yavuz. All rights reserved.
//
#import "SeyahatLayout.h"
#import "SeyahatLayoutAttributes.h"
#define kNumberOfItemsPerPage 5
static NSString * const BHPhotoAlbumLayoutPhotoCellKind = @"PhotoCell";
@interface SeyahatLayout ()
@property (nonatomic, strong) NSDictionary *layoutInfo;
@end
@implementation SeyahatLayout
- (CGSize)collectionViewContentSize
{
// Ask the data source how many items there are (assume a single section)
id<UICollectionViewDataSource> dataSource = self.collectionView.dataSource;
int numberOfItems = [dataSource collectionView:self.collectionView
numberOfItemsInSection:0];
// Determine how many pages are needed
int numberOfPages = ceil((float)numberOfItems/(float)kNumberOfItemsPerPage);
// Set the size
float pageWidth = self.collectionView.frame.size.width;
float pageHeight = self.collectionView.frame.size.height;
CGSize contentSize = CGSizeMake(numberOfPages * pageWidth, pageHeight);
return contentSize;
}
#pragma mark - Layout
- (void)prepareLayout
{
NSMutableDictionary *newLayoutInfo = ;
NSMutableDictionary *cellLayoutInfo = ;
NSInteger sectionCount = ;
NSIndexPath *indexPath = ;
for (NSInteger section = 0; section < sectionCount; section++) {
NSInteger itemCount =
;
for (NSInteger item = 0; item < itemCount; item++) {
indexPath = ;
SeyahatLayoutAttributes* attr =
;
// UICollectionViewLayoutAttributes *itemAttributes =
//[UICollectionViewLayoutAttributes
// layoutAttributesForCellWithIndexPath:indexPath];
// itemAttributes.frame = ;
;
}
}
newLayoutInfo = cellLayoutInfo;
self.layoutInfo = newLayoutInfo;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:
(NSIndexPath *)indexPath {
return self.layoutInfo;
}
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
//NSInteger itemCount = ;
//NSMutableArray* attributes = ;
//for (NSInteger i=0 ; i < itemCount; i++) {
// NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i
inSection:0];
// [attributes addObject:
];
//}
// return attributes;
NSLog(@"RECTX: %f",rect.origin.x );
NSLog(@"RECTY: %f",rect.origin.y );
NSLog(@"RECTWIDTH: %f",rect.size.width );
NSLog(@"RECTHEIGHT: %f",rect.size.height );
id<UICollectionViewDataSource> dataSource = self.collectionView.dataSource;
int numberOfItems = [dataSource collectionView:self.collectionView
numberOfItemsInSection:0];
NSMutableArray* attributes = ;
int page = rect.origin.x / 1024.0;
if(numberOfItems>0){
for(NSInteger i = 0; i<kNumberOfItemsPerPage; i++){
int x = page*kNumberOfItemsPerPage + i;
NSLog(@"%d",x);
NSIndexPath* indexPath = ;
];
}
return attributes;
}
else
return nil;
}
- (SeyahatLayoutAttributes *)layoutAttributesForItemAtIndex:(int)index
{
NSIndexPath *path = ;
SeyahatLayoutAttributes *attributes =
(SeyahatLayoutAttributes *)[UICollectionViewLayoutAttributes
layoutAttributesForCellWithIndexPath:path];
// Figure out what page this item is on
int pageNumber = floor((float)index / (float) kNumberOfItemsPerPage);
// Set the horizontal offset for the start of the page
float pageWidth = self.collectionView.frame.size.width;
float horizontalOffset = pageNumber * pageWidth;
// Now, determine which position this cell occupies on the page.
int indexOnPage = index % kNumberOfItemsPerPage;
int column = 0;
switch (indexOnPage) {
case 0:
case 1:
column = 0;
break;
case 2:
case 3:
case 4:
column = 3;
break;
default:
column = 0;
break;
}
int row = 0;
switch (indexOnPage) {
case 0:
case 2:
row = 0;
break;
case 3:
row = 1;
break;
case 1:
case 4:
row = 2;
break;
default:
row = 0;
break;
}
horizontalOffset = horizontalOffset + ((175.0 + 30.0) * column) + 30.0;
float verticalOffset = (194.0 + 30.0) * row + 30.0;
// finally, determine the size of the cell.
float width = 0.0;
float height = 0.0;
switch (indexOnPage) {
case 0:
width = 585.0;
height = 418.0;
break;
case 1:
width = 585.0;
height = 194.0;
break;
default:
width = 350.0;
height = 194.0;
break;
}
CGRect frame = CGRectMake(horizontalOffset, verticalOffset, width, height);
;
return attributes;
}
+ (Class)layoutAttributesClass
{
return ;
}
@end
</code></pre>
<p>这是我自定义的 uicollectionviewcell 代码;</p>
<pre><code>//
//SeyahatCell.m
//SeyahatTrend
//
//Created by Mehmet Can Yavuz on 03.06.2013.
//Copyright (c) 2013 Mehmet Can Yavuz. All rights reserved.
//
#import "SeyahatCell.h"
#import "PNCollectionCellBackgroundView.h"
@implementation SeyahatCell
- (id)initWithFrame:(CGRect)frame
{
self = ;
if (self) {
// Initialization code
//self.backgroundColor = ;
PNCollectionCellBackgroundView* backgroundView =
[ initWithFrame:frame];
self.backgroundView = backgroundView;
self.imageView =
[ initWithFrame:CGRectMake(5,
5,
frame.size.width-10,
frame.size.height-10)];
;
}
return self;
}
</code></pre>
<p>还有我的 cellForItemAtIndexPath 方法;</p>
<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SeyahatCell *seyahatCell =
[collectionView dequeueReusableCellWithReuseIdentifier:SeyahatCellIdentifier
forIndexPath:indexPath];
Post * p = ;
;
if(p.thumbnail){
];
}
else{
;
}
return seyahatCell;
}
</code></pre>
<p>我该如何解决这个问题?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试使用现有的布局解决方案。为此,我更喜欢使用 <a href="https://github.com/bryceredd/RFQuiltLayout" rel="noreferrer noopener nofollow">https://github.com/bryceredd/RFQuiltLayout</a> </p></p>
<p style="font-size: 20px;">关于ios - 自定义 UICollectionViewLayout 不同的单元格大小,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16934781/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16934781/
</a>
</p>
页:
[1]