首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编程UICollectionViewCell

编程UICollectionViewCell
EN

Stack Overflow用户
提问于 2016-02-08 07:07:15
回答 2查看 696关注 0票数 0

我试图在不使用故事板的情况下以编程方式创建一个单元格,但我遇到了一些问题。单元格代码如下。

代码语言:javascript
运行
复制
import UIKit

class ProductCategoryCollectionViewCell: UICollectionViewCell {


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.setupCell()
}


var productCategoryImageView :UIImageView!

func setupCell(){
    //seting up the imageView as subView
    productCategoryImageView = UIImageView()
    productCategoryImageView.translatesAutoresizingMaskIntoConstraints = false
    let subViewDictionary = ["productCategoryImageViewKey" : productCategoryImageView]


    let productCategoryImageViewWidth = contentView.bounds.size.width
    let productCategoryImageViewHeight = contentView.bounds.size.height
    let productCategoryImageViewHorizontalConstrain = NSLayoutConstraint.constraintsWithVisualFormat("H:[productCategoryImageViewKey(\(productCategoryImageViewWidth))]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: subViewDictionary)
    let productCategoryImageViewVerticalConstrain = NSLayoutConstraint.constraintsWithVisualFormat("V:[productCategoryImageViewKey(\(productCategoryImageViewHeight))]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: subViewDictionary)

    productCategoryImageView.addConstraints(productCategoryImageViewHorizontalConstrain)


    productCategoryImageView.addConstraints(productCategoryImageViewVerticalConstrain)



    contentView.addSubview(productCategoryImageView)
    contentView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.4)
}

}

我正在尝试创建一个subImageView,它将是单元格的大小,但是它不会缩放到单元格的大小。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-08 07:31:41

调用init方法时不知道单元格的大小。因此,将宽度和高度设置为contentView.bounds.size.widthcontentView.bounds.size.height将无法工作。

不必为水平和垂直大小添加约束,您可以添加左、右、顶和底约束,如下所示:

代码语言:javascript
运行
复制
let productCategoryImageViewHorizontalConstrain = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[productCategoryImageViewKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: subViewDictionary)
let productCategoryImageViewVerticalConstrain = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[productCategoryImageViewKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: subViewDictionary)

在添加约束之前,还应该调用contentView.addSubview(productCategoryImageView)

票数 0
EN

Stack Overflow用户

发布于 2016-02-08 07:23:30

/*以编程方式创建一个UICollectionView单元,如上面的代码*/

代码语言:javascript
运行
复制
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    uicollection=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [uicollection setDataSource:self];
    [uicollection setDelegate:self];

    [uicollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [uicollection setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:uicollection];


    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 15;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor greenColor];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 50);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35264049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档