我想使用UICollectionView在iPhone上的网格中显示图像,每行显示3个图像.对于"死的简单测试"(我认为),我已经为我的项目添加了15个JPG图像,因此它们将在我的包中,我可以通过[UIImage imageNamed:...]加载它们.
我想我已经做了一切正确的事情(设置和注册UICollectionViewCell子类,使用UICollectionViewDataSource协议方法),但是,UICollectionView表现得非常奇怪:
它仅显示以下模式中的一些图像:第一行显示图像1和3,第二行显示空白,下一行显示第一行(图像1和3显示正确),第四行显示空白等等. .
如果我在我的NavBar中按下一个触发[self.collectionView reloadData]的按钮,则随机单元格会出现或消失.让我疯狂的是,这不仅是图像出现与否的问题.有时,图像也在单元格之间交换,即它们出现在indexPath中,它们绝对没有连线!
这是我的单元格代码:
@interface AlbumCoverCell : UICollectionViewCell
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@end
@implementation AlbumCoverCell
@synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:frame];
[self.contentView addSubview:_imageView];
}
return self;
}
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)prepareForReuse
{
[super prepareForReuse];
self.imageView.image = nil;
}
@end
Run Code Online (Sandbox Code Playgroud)
我的UICollectionViewController子类的部分代码,其中'imageNames'是一个包含所有jpg文件名的NSArray:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[AlbumCoverCell class] forCellWithReuseIdentifier:kAlbumCellID];
}
#pragma mark - UICollectionViewDataSource Protocol …Run Code Online (Sandbox Code Playgroud) 正如标题所示,我有一个UICollectionView与UIViewControllerIB通过的联系.我有一个UICollectionView叫做的子类imageCell.在viewcontroller viewDidLoad方法中,我注册了这样的类:
CGRect collectionViewRect = CGRectMake(384, 60, 728, 924);
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
imageCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:flow];
[imageCollectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"Cell"];
imageCollectionView.delegate = self;
imageCollectionView.dataSource = self;
Run Code Online (Sandbox Code Playgroud)
然后我调用一个名为getPhotosin viewDidAppear(因为UI原因)的方法,该方法从服务中获取照片,然后我[imagCcollectionView reloadData];在里面调用getPhotos.
然后在cellForItemAtIndexPath我做以下事情:
imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
GRKAlbum *album = [albums objectAtIndex:indexPath.item];
cell.titleLabel.text = album.name;
return cell;
Run Code Online (Sandbox Code Playgroud)
我知道我在这里做错了什么,因为这个方法被调用而cell不是nil,也不是album.
所以,当我运行所有这些代码时,会UICollectionView出现,但没有任何内容.我只能看到背景颜色collectionView …
我有一个集合视图,我试图从didSelect方法的集合视图中删除一个单元格.我成功地使用以下方法
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
Run Code Online (Sandbox Code Playgroud)
但现在我需要从CollectionView Cell中删除按钮上的项目.这里我只得到indexpath.row.由此我无法删除该项目.我试过这样的.
-(void)remove:(int)i {
NSLog(@"index path%d",i);
[array removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
[colleVIew reloadData];
}
Run Code Online (Sandbox Code Playgroud)
但它需要重新加载CollectionView.So删除后的单元格排列动画不存在.请提出一个想法..提前谢谢
我希望我的UICollectionViewCells有圆角和阴影,但我遇到了一个问题,似乎我只能有一个或另一个,但不是两个.
为了绕过角落我在单元格的初始化中使用此代码:
CALayer *layer = [self layer];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
Run Code Online (Sandbox Code Playgroud)
要添加一个投影,我在单元格的初始化中使用此代码:
CALayer *layer = [self layer];
[layer setMasksToBounds:NO];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0f,0.5f)];
[layer setShadowRadius:8.0f];
[layer setShadowOpacity:0.2f];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
Run Code Online (Sandbox Code Playgroud)
要尝试使用圆角和投影,我在单元格的初始化中使用此代码:
CALayer *layer = [self layer];
[layer setMasksToBounds:NO];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0f,0.5f)];
[layer setShadowRadius:8.0f];
[layer setShadowOpacity:0.2f];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
Run Code Online (Sandbox Code Playgroud)
但这只会产生阴影.
这是一个错误还是我做错了什么?
我正在使用UICollectionView,我需要在单击按钮后滚动到第一个单元格.
该按钮位于我的集合视图中的(大)标题部分...当点击它时,我需要滚动到位于此按钮下方的第一个单元格.
我创建了这个按钮的动作,但我不知道如何滚动到第一个UICollectionViewCell.
有人能帮我吗?
我想减少行中单元格之间的大小.现在它看起来像:
我正在尝试这个,以减少它们之间的大小:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
Run Code Online (Sandbox Code Playgroud)
但它对我没有帮助.我做错了什么?
我在UIViewController中有一个UICollectionView.在collectionView cellForItemAtIndexPath:方法中,它基于数据源创建一系列自定义单元格.自定义单元格又包含一个UIView,子类用于绘制单个PDF页面.
它的设置方式是将PDF文件拆分为单页,因此单元格1包含PDF页面1,单元格2包含PDF页面2,依此类推.到目前为止很好,这是我的问题:
当我向下滚动时,UICollectionView开始显示错误的单元格.例如,在34页文档中,它以正确的顺序显示单元格/页面1-16,但随后开始显示似乎已经进一步出列的页面,例如单元格1,单元格2,单元格4.我从未到达任何地方靠近牢房/第34页.
我在过去看过UITableView的类似行为,并认为它与单元格的出列或委托方法有关.不太确定 - 任何帮助表示赞赏.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//create custom cell
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//set file name (always the same; one PDF file)
cell.fileName = fileName;
cell.backgroundColor = [UIColor clearColor];
//set the title to the page number
cell.title = [NSString stringWithFormat:@"page %@", [countArray objectAtIndex:indexPath.row]];
//set the current page (which indicates which page to display) according to the pageCount
cell.currentPage = [[countArray objectAtIndex:indexPath.row] intValue];
return cell; }
Run Code Online (Sandbox Code Playgroud) 我的SuerClass UICollectionViewCell有一个属性:
var selected: Bool
Run Code Online (Sandbox Code Playgroud)
我的班级是
MyClass : UICollectionViewCell {
func setSelected(selected: Bool) {
super.selected = selected
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
前者在XCode 6.2中运行良好,但在XCode 6.3Beta2中它引发了一个错误:
Method 'setSelected' with Objective-C selector 'setSelected:' conflicts with setter for 'selected' from superclass 'UICollectionViewCell' with the same Objective-C selector
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题与XCode 6.3 beta2一起使用?
编辑:我也试过了
override func setSelected(selected: Bool) {
super.selected = selected
// do something
}
Run Code Online (Sandbox Code Playgroud)
这导致错误:
var selected: Bool
Run Code Online (Sandbox Code Playgroud) 最近,我遇到了Interface Builder无法解决的问题.我正在使用UICollectionViewController,每当我添加一个Cell时,它会立即显示一个橙色矩形(表示自动布局更新).更新帧/单元格不会改变任何内容.与橙色标记相关的警告显示"预期:高度= Y,实际:高度= X",其中Y 始终 精确为 X-100.也就是说:当我将自定义单元格高度设置为193时,它会显示"Expected:height = 93".无论我输入哪种尺寸,都会发生这种情况
这个问题是我对子视图及其约束有很多困难.例如,如果我添加一个带有约束的子视图以保持与单元格相同的高度,它将显示为93(预期高度),即使我的单元格应为193. Layouts也变得不可能.
我尝试添加一个新单元格,同样的问题.我尝试删除每个子视图并重新添加它们,没有.
任何想法都非常受欢迎.
非常感谢你!
ios ×7
swift ×4
calayer ×1
inheritance ×1
ios6 ×1
ios9 ×1
iphone ×1
objective-c ×1
overriding ×1
pdf ×1
uiimageview ×1
xcode6 ×1