我知道这对UITableViewCells来说相当容易,但我不知道如何使用UICollectionView来解决这个问题.
编辑.图片澄清.单元格的文本内容在这里不一样,但它们应该是.在景观:

在肖像中:

我尝试根据方法中索引路径的row属性,天真地将单元格文本标签的颜色切换为单元格的背景颜色cellForItemAtIndexPath:.但是,Index Path的row属性实际上不是UICollectionViewFlowLayout中的一行.
所以我们都可能看到了Facebook从视图中删除信使聊天头的方法,我想实现类似的删除方法,但是要集合视图中的集合视图单元格.当长按一个单元格时,视图底部会出现一个框,用户可以将单元格拖动到框中进行删除,或者只是松开选择单元格,从而返回原始单元格地位和国家.
我知道有两个库允许拖动和重新排序集合视图单元,并试图使用它们来实现这样的功能,但是它们允许在集合视图中完整地重新排序单元格,并且我认为可能存在用于实现我所需功能的库,或更简单的方法.
所以基本上我的问题是,在集合视图中实现此功能的最佳方法是什么,如果您知道可以轻松实现此功能的库,那么如果您可以共享它就会很棒.
代码示例总是受欢迎,让事情变得更容易.
集合视图单元格重新排序库:LXReorderableCollectionViewFlowLayout和DraggableCollectionView
我理解如何删除集合视图单元格,我对删除方法更感兴趣.*
xcode objective-c ios uicollectionview uicollectionviewlayout
我有一个自定义单元格的UICollectionView.我完成了这项工作,直到我尝试以编程方式而不是故事板创建集合视图.我这样做是因为我无法为不同的屏幕尺寸更改集合视图的帧大小.所以我尝试以编程方式创建一个集合视图.我的问题是我的自定义单元格不起作用.对于我的自定义单元格,我在视图中绘制一个圆圈,用户cornerradius在单元格的视图上,有一个UILabel和一个UIImageView.
我画了圆圈和执行cornerradius中的东西drawRect:定制细胞.其余的ie:将图像放在ImageView中,将文本放在UILabel中,我是在collectionView数据源方法中完成的.
我遇到的问题是我没有得到这个cornerradius东西,虽然奇怪的是我可以在视图上画一个圆圈.第二个问题是我没有在图像视图中获取图像而标签中没有文本
这是我的代码.我在ViewDidload:方法中创建集合视图.
 [layout setSectionInset:UIEdgeInsetsMake(24, 10, 24, 10)];
 [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
 [layout setMinimumLineSpacing:15];
 [layout setMinimumInteritemSpacing:10];
 self.collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
 self.collectionView.delegate=self;
 self.collectionView.dataSource=self;
 [self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:@"cell"];
 self.collectionView.backgroundColor= [UIColor blackColor];
 self.searchBar.delegate = self;
 self.locations = [[NSArray alloc]init];
 self.location = [[NSString alloc]init];
 [self.view addSubview:self.collectionView];
这是drawRect:我的customCell.m
- (void)drawRect:(CGRect)rect
{
    UIBezierPath *circularpath = [[UIBezierPath alloc]init];
    CGRect Rect = CGRectMake(6, 20, 130, 130);//338
    CGPoint mypoint = CGPointMake(Rect.origin.x + (Rect.size.width / 2), Rect.origin.y + …objective-c ios uicollectionview uicollectionviewcell uicollectionviewlayout
我有一个UICollectionViewFlowLayout具有estimatedItemSize集.
当我运行我的应用程序时,一切正常,但在我的单元测试中,EXC_ARITHMETIC每次击中我都会崩溃collectionView:dequeueReusableCellWithReuseIdentifier:.
我知道这是因为我estimatedItemSize删除它修复了崩溃.
有谁知道为什么会这样?
谢谢
一个人可以invalidateLayout直接调用一个UICollectionView或一个可以实现shouldInvalidateLayoutForBoundsChange:并返回YES。
现在,我发现在我的实现中,两者的工作方式有所不同。invalidateLayout在我的情况下可以正常工作,但是shouldInvalidateLayoutForBoundsChange:行为有所不同,因此布局看起来被破坏了。详细地说,在方向更改时,单元的宽度不会更新,因此minimumLineSpacing和minimumInteritemSpacing并不相同,这不适用于我的布局。特别是,由于未更新宽度,连续的项目数是错误的。
如果我查看文档,invalidateLayoutWithContext:则shouldInvalidateLayoutForBoundsChange:返回时称为YES。但是,此方法与直接调用invalidateLayout有何不同?
我有一个任务,我要准备一个简单的GridView,几乎没有约束.我想过使用UICollectionView并且还对网做了一些研究.我在这里发现了一篇博文,其内容与我希望实现的类似.
现在,它使用customCollectionViewCell行和列的类.每个细胞都有一个UILabel.UILabel因此,这可以具有动态文本,单元格应根据动态调整其大小UILabel.
请找到以下所有代码 -
UICollectionView 用于行单元格
class RowCell: UICollectionViewCell
{
    var textLabel : UILabel!
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        textLabel = UILabel(frame: CGRectMake(0, 0, 65, 35))
        textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
        textLabel.textAlignment = .Center
        textLabel.numberOfLines = 0
        textLabel.backgroundColor = UIColor.clearColor()
        textLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping
        textLabel.sizeThatFits(CGSizeMake(self.bounds.width, self.bounds.height))
        contentView.addSubview(textLabel)
        self.layer.borderWidth = 0.7
        self.layer.borderColor = charcoalColor.CGColor
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, textLabel.frame.width*8, textLabel.frame.height*8)
    }
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented") …ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
我有一个自定义布局的collectionView,到目前为止我只有一个UICollectionReusableView.起初一切正常:
prepareLayout() 被称为填充缓存, collectionViewContentSize() 被调用并返回正确的cententSize和 layoutAttributesForElementsInRect() 被调用并返回正确的属性. 这会导致viewForSupplementaryElementOfKind调用collectionView的-method,之后视图变为可见.
稍后,当我按下按钮时,可重用视图的大小会更改,并被collectionView.reloadData()调用.这会导致再次调用上面提到的3个方法,并且它们仍然会返回具有新大小的正确值,但这次它不会触发viewForSupplementaryElementOfKind,并且collectionView变为空.
为什么不viewForSupplementaryElementOfKind叫?
调试示例:
layoutAttributesForElementsInRect第一次收集视图的返回(即工作时)
layout attributes: [<UICollectionViewLayoutAttributes: 0x7fa5005dc970> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 414 1106); ]
layoutAttributesForElementsInRect第二次返回集合视图的布局(即当它不起作用时)
layout attributes: [<UICollectionViewLayoutAttributes: 0x7fa500708f00> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 414 1560.67); ]
编辑:调试:
我刚刚测试使用collectionView.reloadData() …
我只是遇到了一个关于a的行为的非常奇怪的问题UICollectionView,我认为集合视图和我都非常困惑。
我在项目中实现了一个具有水平滚动方向的特定收集视图,不久之后,质量保证发布了一个错误通知,指出在某些设备上收集视图项之间的间距不正确。我进一步检查了该错误,并意识到问题不仅在特定设备上仍然存在,而且在运行iOS 9的所有设备上仍然存在。在iOS 10上,一切正常。
因此,我创建了一个简单的示例项目来隔离可以在GitHub上找到的潜在问题:
首先,我对示例项目没有显示iOS 9错误感到更加困惑。但是,一旦我将其设置为estimatedItemSize非零值以使用自动调整大小的单元格(即“自动布局”),就会再次出现相同的不正确间距。
我发现只有在以下特定情况下(iOS 9 +estimatedItemSize != .zero),集合视图才会使,minimumLineSpacing和混淆minimumInteritemSpacing,至少在实现各自的流布局委托函数时:
collectionView(_:layout:minimumLineSpacingForSectionAt:)
collectionView(_:layout:minimumInteritemSpacingForSectionAt:)
因此,无论何时要指定这些间距之一,都必须实现另一个间距。
对我来说,这似乎是一个严重的系统错误,但这是一件大事,我无法相信没有其他人会遇到此问题。因此,我更有可能在代码中或在理解这些间距的工作方式时犯了一些错误(尽管在Apple Docs中已经很清楚地描述了上面链接的委托方法)。
示例项目中的“使用自动布局”开关在(禁用)和(启用)estimatedItemSize之间切换。.zeroCGSize(width: 100, height: 50)
如《 iOS的Collection View编程指南》中所述,如果要将新的补充视图或装饰视图添加到Flow Layout,则必须将UICollectionViewFlowLayout子类化。
我需要帮助来在Swift 3中实现以下步骤:
标准流布局类仅支持节标题和节脚视图,不支持装饰视图。为了支持其他补充视图和装饰视图,您至少需要重写以下方法:
- layoutAttributesForElementsInRect :(必填)
- layoutAttributesForItemAtIndexPath :(必填)
- layoutAttributesForSupplementaryViewOfKind:atIndexPath :(以支持新的补充视图)
- layoutAttributesForDecorationViewOfKind:atIndexPath :(以支持新的装饰视图)
在您的layoutAttributesForElementsInRect:方法中,您可以调用super获得单元格的布局属性,然后为指定矩形中任何新的补充或装饰视图添加属性。使用其他方法按需提供属性。
我想实现的目标:
referenceSizeForHeaderInSection,用于在集合视图控制器中定义新补充视图的大小(return CGSize.zero如果不应包含“ item header”)我知道这个问题定义得太广泛了。但是在Devon的帮助下和一些自己的想法,我找到了一个对我有用的解决方案。
子类UICollectionViewFlowLayout
class SBCollectionViewFlowLayout: UICollectionViewFlowLayout { … }
重写layoutAttributesForElements(in rect: CGRect)以将附加补充视图添加到每个单元格。
// Check if there already valid layout attributes
guard var layoutAttributes = super.layoutAttributesForElements(in: rect) else { return nil }
// Try to append new supplementary view to …ios uicollectionview uicollectionviewlayout uicollectionreusableview swift
我有一个UICollection视图,看起来像这样(见两个紫色和蓝色边框):
在iOS 10上没有标题/空白,但在iOS11上有.我已经尝试了这里提到的所有内容:如何以编程方式在UICollectionView中启用/禁用节头?
我的代码目前看起来像这样:
- (void) viewDidLoad
{
    [super viewDidLoad];
    autocompleteLayout = [[SQLProAutocompleteFlowLayout alloc] init];
    autocompleteLayout.headerReferenceSize = CGSizeZero;
    autocompleteCollectionView = [[UICollectionView alloc] initWithFrame: CGRectZero
                                                    collectionViewLayout: autocompleteLayout];
    autocompleteCollectionView.insetsLayoutMarginsFromSafeArea = NO;
    [self.view addSubview: autocompleteCollectionView];
    // removed layout code (leftAnchor, rightAnchor, topAnchor, bottomAnchor).
    autocompleteCollectionView.delegate = self;
    autocompleteCollectionView.dataSource = self;
    // Removed nib register code
    self.view.layer.borderColor = [UIColor redColor].CGColor;
    self.view.layer.borderWidth = 1;
    autocompleteCollectionView.layer.borderColor = [UIColor blueColor].CGColor;
    autocompleteCollectionView.layer.borderWidth = 2;
}
- (CGSize) collectionView: (UICollectionView *) collectionView
                   layout: (UICollectionViewLayout *) collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section
{
    return …