标签: uicollectionviewcell

未定义UICollectionViewFlowLayout的行为,因为单元格宽度大于collectionView宽度

2015-08-18 16:07:51.523示例[16070:269647]未定义UICollectionViewFlowLayout的行为,因为:2015-08-18 16:07:51.523
示例[16070:269647]项目宽度必须小于UICollectionView的宽度减去左边和右边的值,减去左边和右边的内容值.
2015-08-18 16:07:51.524示例[16070:269647]相关的UICollectionViewFlowLayout实例是,并附加到; 动画= {position =; bounds.origin =; bounds.size =; }; layer =; contentOffset:{0,0}; contentSize:{1024,770}>集合视图布局:.2015-08-18 16:07:51.524示例[16070:269647]在UICollectionViewFlowLayoutBreakForInvalidSizes上创建一个符号断点,以便在调试器中捕获它.

这就是我得到的,我所做的是

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(self.collectionView!.frame.size.width - 20, 66)
    }
Run Code Online (Sandbox Code Playgroud)

当我从横向旋转到纵向时,控制台仅在iOS 9中显示此错误消息,是否有人知道这是怎么回事?如果有解决方法?

截图

uicollectionview uicollectionviewcell uicollectionviewlayout ios9 xcode7-beta5

47
推荐指数
9
解决办法
5万
查看次数

使用xib创建UICollectionViewCell子类

我正在尝试创建一个UICollectionViewCell链接了一个xib 的子类,我已经这样做了:我创建了一个新的xib文件并UICollectionViewCell在其中添加了一个,然后我创建了这个子类文件:

@interface MyCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UILabel *label;
@end
Run Code Online (Sandbox Code Playgroud)

另外我在文件所有者自定义类中链接了MyCell接口构建器中的类,并且我添加了一个UILabel,然后在我的UICollectionViewviewDidLoad中我这样做:

[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];
Run Code Online (Sandbox Code Playgroud)

除此之外:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];


cell.label.text = @"Cell Text";


return cell;
}
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,我收到此错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the …
Run Code Online (Sandbox Code Playgroud)

iphone xib ios uicollectionview uicollectionviewcell

46
推荐指数
2
解决办法
6万
查看次数

强制施展真的很糟糕,应该总是避免吗?

我开始使用swiftLint并注意到Swift的最佳实践之一是避免使用强制转换.但是在处理tableView,collectionView for cells时我经常使用它:

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! MyOffersViewCell
Run Code Online (Sandbox Code Playgroud)

如果这不是最好的做法,那么处理这个问题的正确方法是什么?我想我可以使用if with as?,但这是否意味着其他条件我需要返回一个空单元格?那可以接受吗?

if let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as? MyOffersViewCell {
      // code
} else {
      // code
}
Run Code Online (Sandbox Code Playgroud)

uitableview tableview collectionview uicollectionviewcell swift

38
推荐指数
5
解决办法
2万
查看次数

创建UICollectionViewCell时子视图帧不正确

问题

我创建了一个带有自定义UICollectionViewCell的UICollectionViewController.自定义单元格包含一个大的矩形UIView(名为colorView)和一个UILabel(名为nameLabel).

首次使用单元格填充集合并打印colorView.frame时,打印的帧具有不正确的值.我知道它们是不正确的,因为colorView帧大于单元框架本身,即使colorView被正确绘制.

但是,如果我滚动collectView足以触发重用以前创建的单元格,colorView.frame现在具有正确的值!我需要正确的帧,因为我想将圆角应用于colorView图层,我需要正确的coloView大小才能执行此操作.顺便说一下,如果你想知道,colorView.bounds也有与colorView.frame相同的错误大小值.

这个问题

创建单元格时为什么帧不正确?

现在一些代码

这是我的UICollectionViewCell:

class BugCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var colorView: UIView!
    @IBOutlet weak var nameLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)

这是UICollectionViewController:

import UIKit

let reuseIdentifier = "Cell"
let colors = [UIColor.redColor(), UIColor.blueColor(),
              UIColor.greenColor(), UIColor.purpleColor()]
let labels = ["red", "blue", "green", "purple"]

class BugCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colors.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> …
Run Code Online (Sandbox Code Playgroud)

xcode ios uicollectionview uicollectionviewcell swift

36
推荐指数
4
解决办法
2万
查看次数

在哪里突出显示UICollectionViewCell:委托还是单元格?

根据Collection View编程指南,应该处理单元格高光的视觉状态UICollectionViewDelegate.像这样:

- (void)collectionView:(PSUICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell highlight];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell unhighlight];
}
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种方法的是它为委托添加了非常特定于单元格的逻辑.事实上,UICollectionViewCell通过highlighted财产独立管理其突出显示的状态.

那么,压倒一切setHighlighted:不是一个更清洁的解决方案吗?

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if (highlighted) {
        [self highlight];
    } else {
        [self unhighlight];
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法有没有缺点而不是委托方法?

ios ios6 uicollectionview uicollectionviewcell

34
推荐指数
5
解决办法
3万
查看次数

如何以编程方式在UICollectionView中启用/禁用节标题?

如何以编程方式在UICollectionView中启用/禁用节标题?

它可以在Storyboard(复选框)中轻松完成,但是如何在代码中完成呢?

ios uicollectionview uicollectionviewcell

34
推荐指数
2
解决办法
2万
查看次数

UICollection视图调整屏幕大小的单元格大小

目前我的收藏视图占据了我3.5英寸屏幕尺寸的大部分屏幕.收集视图从顶部开始是51像素,从屏幕底部开始是20像素.我正在使用单元格UIPageControl创建分页布局,每页一个单元格.当切换到4英寸大小时,使用约束我可以使集合视图扩展到新空间(保持从顶部51个像素和从底部20个),但单元格大小保持不变,所以现在有一堆细胞顶部和底部的空白空间.我尝试使用约束,但看起来你不能为细胞设置它们.如何扩大单元格大小以填充iPhone 5上的空白区域?

集合视图代码就是

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
return cell;

}
Run Code Online (Sandbox Code Playgroud)

布局在IB中设置我希望在添加更多之前首先使布局工作

iPhone 5查看. 我想填补空白空间

iPhone 4查看

constraints objective-c ios uicollectionview uicollectionviewcell

33
推荐指数
2
解决办法
6万
查看次数

UICollectionview加载单元格时滚动波动

我在我的应用程序中使用了一个库UICollectionView.细胞大小约为70,70.我使用ALAssetsALAssetLibrary我在其中已经存储在列表中的画廊.

我使用通常的模式来填充单元格:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

  mycell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
  mycell.imageView.image = [[UIImage imageWithCGImage:[alassetList objectAtIndex:indexpath.row] thumbnail]];
  return mycell;
}
Run Code Online (Sandbox Code Playgroud)

我的画廊滚动波涛汹涌.我不明白为什么会这样.我已经尝试添加一个NSCache来缓存缩略图(想想可能创建图像很昂贵),但这对性能没有帮助.

我希望用户界面像股票应用程序一样黄油.

我现在怀疑它可能是某种东西UICollectionViewCell prepareForReuse可能会阻碍dequeueReusableCellWithReuseIdentifier方法,但使用我无法找到的工具.

还有其他可能造成这种情况的事吗?是否有一种"更快"的方式以更快的方式UICollectionViewCelldequeue它们做准备?

objective-c ios xcode-instruments uicollectionview uicollectionviewcell

33
推荐指数
3
解决办法
4万
查看次数

如何在UICollectionViewCell上显示标准复选标记?

我正在设计一个带有UICollectionView的iOS应用程序,我希望用户能够在此视图中选择多个项目.似乎Apple在这种情况下使用了标准的复选标记.例如,在下图中,您可以在共享表中选择多张照片时看到它.

根据文档,您负责更新单元格的UI以反映其选择状态.我知道在UITableViewCell上你可以设置accessoryType属性来添加一个复选标记,但我似乎找不到任何UICollectionViewCell的等价物.

有没有Apple提供的方式在我的应用程序中使用此复选标记,除了试图从屏幕截图中删除此图标?

在此输入图像描述

cocoa-touch ios uicollectionview uicollectionviewcell

33
推荐指数
3
解决办法
2万
查看次数

UICollectionView,只是适合单元格宽度?

这是一个黄色的简单UICollectionView

在此输入图像描述

红色箭头设置单元格的宽度.(TBC:单击粉红色单元格:对于"大小"选择"默认",然后您在红色箭头处设置的内容将变为单元格的大小.)

例如,直立的iPhone设置宽度为320.

但这看起来很疯狂......当然我可以根据UICollectionView的宽度设置单元格宽度?

自动调整视图本身没有问题.

在此输入图像描述

这很好.

但似乎很疯狂,我无法将CELL的宽度设置为"与视图相同".似乎很难相信人们必须在代码中手动设置它?

TBC换句话说,正如尼基塔指出的那样,

-(CGSize) collectionView:(UICollectionView *)collectionView
     layout:(UICollectionViewLayout *)collectionViewLayout
     sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    return self.view.frame.size;
    }
Run Code Online (Sandbox Code Playgroud)

(事实上​​你通常也需要这个...)

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
   layout:(UICollectionViewLayout*)collectionViewLayout
   insetForSectionAtIndex:(NSInteger)section
    {
    return UIEdgeInsetsMake(0,0,0,0);   //t,l,b,r
    }
Run Code Online (Sandbox Code Playgroud)

事实上 - 你必须在代码中做到这一点?!在Storyboard中,没有办法在自动布局或其他任何事情上做到这一点?!

ios uicollectionview uicollectionviewcell

32
推荐指数
1
解决办法
3万
查看次数