标签: uicollectionviewcell

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万
查看次数

UICollectionView estimatedItemSize - 最后一个单元格未对齐

我想在单元格中使用estimatedItemSize和preferredLayoutAttributesFittingAttributes制作一个通常的horizo​​ntalScrolling flowLayout UICollectionView.但最后一个细胞有问题.知道问题出在哪里? 项目本身

在此输入图像描述

@implementation RowCollectionView

- (instancetype) initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:frame collectionViewLayout:layout])
    {
        [self configureRowCollectionView];
    }

    return self;
}

- (void) awakeFromNib
{
    [super awakeFromNib];

    [self configureRowCollectionView];
}

- (void) configureRowCollectionView
{
    self.backgroundColor = [UIColor lightGrayColor];

    self.dataSource = self;
    self.delegate = self;

    // Horizontal Direction
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *) self.collectionViewLayout;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // Estimated Item Size
    flowLayout.estimatedItemSize = CGSizeMake(self.bounds.size.height, self.bounds.size.height);


    [self registerClass:[RowCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([RowCollectionViewCell class])];
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ …
Run Code Online (Sandbox Code Playgroud)

objective-c ios uicollectionview uicollectionviewcell

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

UICollectionViewCell边框/阴影

在构建iPad应用程序时,如何在UICollectionViewCell周围绘制边框?

更多细节:我实现了一个扩展UICollectionViewCell的ProductCell类.现在,我想分配一些奇特的细节,例如边框,阴影等.但是,当试图在这里使用类似的东西时,Xcode告诉我接收器类型'CALayer'是一个前向声明.

objective-c uicollectionview uicollectionviewcell

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

首次加载时UICollectionViewCell内容大小错误

我有一个添加UICollectionView到我的,UIViewController并为它制作了一个自定义单元格.

我使用该sizeForItemAtIndexPath方法设置单元格的大小,并将每个单元格设置为UIScreen.mainScreen().bounds.width/2高度和宽度.基本上,每个单元格都是屏幕宽度的一半,高度相同.

然后我UIImageView在单元格内部,每个边缘固定到单元格的相对边缘,以便图像视图填充单元格.我也有一个UILabel在细胞中垂直和水平居中的.

但是,在第一次加载时,单元格的大小正确,但内容在左上角的小方块中要小得多.(如下图所示,我将单元格的contentView背景颜色设置为绿色,将imageView背景颜色设置为红色).

在此输入图像描述

然后向下滚动一点(我假设一个可重复使用的单元格出列)所有单元格都很好(滚动后再保持正常).

在此输入图像描述

是什么导致内容在首次加载时不能正确约束?

UPDATE

我的一些代码UIViewController:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("PrevDrawCell", forIndexPath: indexPath) as? PrevDrawCell

        if cell == nil {
            cell = PrevDrawCell()
        }

        cell!.drawVC = self
        cell!.imageShortCode = self.tempImageURLs[indexPath.row]

        // Pull image (async) and set after download
        cell!.setupImage()

        cell!.contentView.backgroundColor = UIColor.greenColor()
        cell!.coverView.backgroundColor = UIColor.redColor()

        cell!.dateLabel.font = UIFont(name: "HelveticaNeue-Light", size: 26)
        cell!.dateLabel.text = "\(self.tempImageURLs.count-indexPath.row)/9"

        return cell! …
Run Code Online (Sandbox Code Playgroud)

objective-c ios uicollectionview uicollectionviewcell swift

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

如何以编程方式创建UICollectionViewCell

我正在尝试以UICollectionView编程方式创建.我需要在单元格内添加标签,所以我创建了CollectionViewCell类.

这是班级:

import UIKit

class MyCollectionViewCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是collectionView实现类:

import UIKit

class TwoViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

    let leftAndRightPaddings: CGFloat = 80.0
    let numberOfItemsPerRow: CGFloat = 7.0
    let screenSize: CGRect = UIScreen.main.bounds
    private let cellReuseIdentifier = "collectionCell"
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", …
Run Code Online (Sandbox Code Playgroud)

uicollectionview uicollectionviewcell swift

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

滚动条错误地出现在UICollectionView部分标题下方

出于某种原因,我的滚动条始终显示在集合视图部分标题下方.任何帮助是极大的赞赏!

在此输入图像描述

uicollectionview uicollectionviewcell swift

29
推荐指数
2
解决办法
3219
查看次数

UICollectionView registerClass:forCellWithReuseIdentifier方法中断UICollectionView

registerClass:forCellWithReuseIdentifier:方法的作用是什么?根据Apple的开发者文档,它应该是

"注册一个类用于创建新的集合视图单元格."

当我尝试使用它我的项目时,我得到一个黑色的集合视图.当我删除它一切正常.

#define cellId @"cellId"
#import "ViewController.h"
#import "Cell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(strong, nonatomic)NSMutableArray * photoArray;


@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@",_photoArray);
    _photoArray = [[NSMutableArray alloc]initWithCapacity:0];
    [_collectionView registerClass:[Cell class] forCellWithReuseIdentifier:cellId];
    for(int i=1;i<=12;i++)
    {
        NSString * imgName = [NSString stringWithFormat:@"%d.png",i];
        UIImage *img  = [UIImage imageNamed:imgName];
        [_photoArray addObject:img];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _photoArray.count;
}

- …
Run Code Online (Sandbox Code Playgroud)

objective-c ios uicollectionview uicollectionviewcell

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

在Swift中使用CollectionView进行网格布局

我想实现这个结果: 在此输入图像描述

搜索我发现可能的方法是使用UICollectionView,所以没有问题,因为有很多关于Stack Overflow的教程和问题.我有3个问题:

  1. 我找不到任何关于"分隔符"(划分所有框的行).我喜欢它不会水平触摸屏幕边框.它是以编程方式完成的吗?

  2. 为了在所有设备中平均分配空间(水平3个盒子/按钮),我找到了这个答案答案.这是正确的方法吗?

  3. 对于模糊效果,我找到了这个答案: 如何在UITableView中使用自适应segue实现UIVisualEffectView

对于TableView它将是:

if (!UIAccessibilityIsReduceTransparencyEnabled()) {
tableView.backgroundColor = UIColor.clearColor()
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
tableView.backgroundView = blurEffectView
}
Run Code Online (Sandbox Code Playgroud)

我可以这样做吗?

     @IBOutlet var collectionView: UICollectionView!
    if (!UIAccessibilityIsReduceTransparencyEnabled()) {
    collectionView.backgroundColor = UIColor.clearColor()
    let blurEffect = UIBlurEffect(style: .Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    collectionView.backgroundView = blurEffectView
    }
Run Code Online (Sandbox Code Playgroud)

gridview ios uicollectionview uicollectionviewcell swift

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

UICollectionViewCell子类init永远不会运行

我在viewDidLoad中添加了一个像这样的集合视图......

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[CameraCell class] forCellWithReuseIdentifier:CameraCellReuseIdentifier];
[self.collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.collectionView];
Run Code Online (Sandbox Code Playgroud)

我有一个名为CameraCell的UICollectionViewCell子类,其中包含一个init ...

- (id)init
{
    self = [super init];
    if (self) {
        // Add customisation here...
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageChanged:) name:@"image" object:nil];

        self.contentView.backgroundColor = [UIColor yellowColor];

        self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.imageView.clipsToBounds = YES;
        [self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.contentView addSubview:self.imageView];

        NSDictionary *views = NSDictionaryOfVariableBindings(_imageView);

        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView]|"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:views]];

        [self.contentView …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview uicollectionviewcell

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

UICollectionView cellForItemAtIndexPath为nil

我有一个更新现有UICollectionView的函数.创建了UICollectionView,我可以看到它,但是当我想访问它的单元格来更新它时,它们是零.

-(void)finishExam{

    for (int i = 0; i < [self.questionsOverviewCollection numberOfItemsInSection:0]; i++) {

        NSLog(@"self.questionsOverviewCollection - %@",self.questionsOverviewCollection);
        NSLog(@"cell - %@",[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]);
        NSLog(@"overviewCell - %@",(OverviewCell*)[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]);
        NSLog(@"numOfCells - %d", [self.questionsOverviewCollection numberOfItemsInSection:0]);

        OverviewCell *cell = (OverviewCell*)[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            [cell finishExam];
    }
}


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

    OverviewCell *cell = (OverviewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    [cell someSetUp];

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

日志:

self.questionsOverviewCollection - <UICollectionView: 0xa1abc00; frame = (14 219; 217 441); clipsToBounds = YES; opaque = NO; autoresize = …
Run Code Online (Sandbox Code Playgroud)

objective-c ios uicollectionview uicollectionviewcell

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