2016-02-08 29 views
0

我想創建一個單元格而不使用故事板,但我偶然發現了一些問題。單元代碼如下。程序UICollectionViewCell編程

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。

回答

0

該電池的尺寸時init方法被稱爲是未知的。因此將寬度和高度設置爲contentView.bounds.size.widthcontentView.bounds.size.height將不起作用。

而不是增加約束你可以添加左水平和垂直尺寸,右,頂部和底部的限制,像這樣的:

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

謝謝...爲什麼我沒有想到它。非常感謝 – progammingBeignner

-1

/*創建在編程一個UICollectionView細胞如上述代碼*/

- (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 
+0

我不明白你在做什麼..我已經有了一個UIcollectionViewControl,並且我已經調用了itemforSize委託方法來將它縮放到一個特定的大小。但是,它是單元格中的子圖像不能縮放到單元格的大小。 – progammingBeignner

+0

這個問題被標記爲「Swift」,而不是「Objective-C」。 – Moritz