我的 UICollectionView 的背景图像在宽度和高度方面总是比 UICollectionView 小。这是图片(背景白色是整个 UICollectionView 大小):
正如您从图片中看到的,这会导致内容超出背景图像。当我向右滚动时,会发生这种情况(我清除了 CollectionView 的背景颜色):
这是 CollectionView 的大小检查器:
如何减小 CollectionView 中单元格容器的大小?
或者解决这个问题的正确方法是什么?
我不知道为什么设计能够适应其内容的单元格如此复杂。它不应该需要那么多代码,我仍然不明白为什么UIKit不能正确处理这个问题。
无论如何,这是我的问题(我已经编辑了整篇文章):
我有一个UICollectionViewCell包含UITableView.
这是我的sizeForItem方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
var cellWidth: CGFloat = collectionView.bounds.size.width
var cellHeight: CGFloat = 0
let cellConfigurator = items[indexPath.item].cellConfigurator
if type(of: cellConfigurator).reuseId == "MoonCollectionViewCell" {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: type(of: cellConfigurator).reuseId, for: indexPath) as? MoonCollectionViewCell {
cell.contentView.layoutIfNeeded()
let size = cell.selfSizedTableView.intrinsicContentSize
cellHeight = size.height
}
}
return CGSize.init(width: cellWidth, height: cellHeight)
}
Run Code Online (Sandbox Code Playgroud)
sizeForItem之前调用过cellForItem …
uitableview ios uicollectionviewcell swift intrinsic-content-size
如何在没有动画的情况下仅重新加载一个单元格。我试着用
horizontalItem.selectedType = horizontalItem.selectedType == .deselected ? .selected : .deselected
collectionView.reloadItems(at: [indexPath])
Run Code Online (Sandbox Code Playgroud)
并更改背景和标题颜色,但更改为动画。
我在 UITableViewCell 内滚动 UICollectionView 时遇到问题。
不幸的是,CollectionView 根本不滚动。当我尝试禁用其他 UITableViewCells 时,它可以正常工作,没有任何问题,反之亦然。
集合视图单元格:
import UIKit
class CategoriesCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
layoutUI()
}
required init(coder adecoder: NSCoder) {
fatalError("init(codeer:) has not been implemented")
}
lazy var categoriesImage: UIImageView = {
let categoriesImage = UIImageView()
categoriesImage.contentMode = .scaleToFill
categoriesImage.layer.cornerRadius = 8.0
categoriesImage.image = UIImage(named: "pizza")
categoriesImage.layer.cornerRadius = 8.0
categoriesImage.layer.masksToBounds = true
categoriesImage.translatesAutoresizingMaskIntoConstraints = false
return categoriesImage
}()
lazy var containerView: UIView = {
let containerView = UIView() …Run Code Online (Sandbox Code Playgroud) 我有一个CGPoint,我想知道我的集合视图中的哪个单元格当前包含该点.有没有简单的方法可以做到这一点,还是我必须编写自己的方法?
objective-c cgpoint ios uicollectionview uicollectionviewcell
在UICollectionView上我想用图像填充CollectionViewCell.但是我得到以下错误:
我的错误在哪里,我该如何解决?
2013-01-29 22:25:38.606 foobar[1277:c07] CollectionViewController loaded with searches: 22
2013-01-29 22:25:38.609 foobar[1277:c07] id: 0 -> content: <UIImage: 0xaa618e0>
2013-01-29 22:25:38.610 foobar[1277:c07] -[UICollectionViewCell cellImageView]: unrecognized selector sent to instance 0x939a7b0
2013-01-29 22:25:38.610 foobar[1277:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell cellImageView]: unrecognized selector sent to instance 0x939a7b0'
*** First throw call stack:
(0x1d9f012 0x139fe7e 0x1e2a4bd 0x1d8ebbc 0x1d8e94e 0x478a 0x7e9a1a 0x7eb034 0x7ed2d1 0x33792d 0x13b36b0 0x2afffc0 0x2af433c 0x2af4150 0x2a720bc 0x2a73227 0x2a738e2 0x1d67afe 0x1d67a3d 0x1d457c2 0x1d44f44 0x1d44e1b 0x1cf97e3 0x1cf9668 0x2e765c …Run Code Online (Sandbox Code Playgroud) 我使用的是UIPanGestureRecognizer一个UIView是一个UICollectionViewCell.我正在使用该translationInView方法获得沿X轴的平移并UIView向左和向右滑动.它工作正常,但现在我无法在CollectionView中上下滚动,如果我的手指在单元格上.有没有办法让PanGestureRecognizer传递垂直滚动到UICollectionView?
我试图复制在Reeder.app for iOS中看到的滑动.
cocoa-touch objective-c uipangesturerecognizer uicollectionview uicollectionviewcell
我有一个带有子视图的uiviewcontroller,它是一个uicollection视图.我的视图控制器实现了所有的collectionview委托.由于某些原因,一些单元格是黑色的(当我滚动它们出现时,似乎是随机的)我的代码是:
#import "NewGalleryViewController.h"
@interface MyCell : UICollectionViewCell
@property (nonatomic,strong) UIImageView *imageView;
@end
@implementation MyCell
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"frame = %@\n", NSStringFromCGRect(frame));
if (self)
{
self.imageView = [[UIImageView alloc] initWithFrame:frame];
[self.contentView addSubview:self.imageView];
}
return self;
}
@end
@interface NewGalleryViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@end
@implementation NewGalleryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional …Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout
我正在尝试添加一个UILabel给我的collectionViewCell.但是,在一些单元格文本开始与之前的单元格数据重叠之后.
更新:这似乎只发生在我的手机(iphone 5)上,但不在模拟器上(2013 macbook air).
如下所示:

我以编程方式实现整个视图.问题似乎与任何元素没有关系collectionview,所以我认为问题也适用于表格视图.我不确定我是否遗漏了以下内容:
if(cell == nil) {// do something }
Run Code Online (Sandbox Code Playgroud)
如果是这样的话,有人可以解释一下它的作用吗?如果这不是问题,我真的不确定是什么导致了这个问题.我也在使用NSMutableAttributedString,但这不是问题,因为我试图插入一个常规字符串并获得相同的结果.
我的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
TTSong *tempSong = [songArray objectAtIndex:indexPath.row];
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 110, 150, 40)];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]];
[cellLabel setFont:[UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]];
[cellLabel setNumberOfLines:2];
NSString * labelString = [[tempSong.artist stringByAppendingString:@"\n"] stringByAppendingString:tempSong.title];
NSMutableAttributedString * attributedString = …Run Code Online (Sandbox Code Playgroud) 我错过了一些明显的东西,因为这不是一项艰巨的任务.
我在故事板(在其他视图中)中有我的Collection视图,原型单元的重用ID为"Cell",在该单元格中有一个UILabel,标记为100.
代码(样板文件):
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = @"Hello";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
代码在a内UICollectionViewController,应该如此.当我运行时,视图显示没有单元格或文本可见.
一个NSLog在cellForItemAtIndexPath确认它被称为4倍.我甚至改变了原型细胞的背景颜色,这表明甚至细胞都没有出现.
在viewDidLoad,它调用: [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
我错过了什么?
ios ×8
swift ×4
objective-c ×3
uitableview ×3
cgpoint ×1
cocoa-touch ×1
selector ×1
uiimage ×1
uiimageview ×1
xcode ×1