2015-02-17 1 views
0

У меня есть UICollectionViewCellМалый UIImage вид в обычае UICollectionViewCell

#import "DBPhotoCollectionViewCell.h" 
#define IMAGEVIEW_BORDER_LENGTH 5 
    @implementation DBPhotoCollectionViewCell 
    - (id)initWithFrame:(CGRect)frame 
    { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self setup]; 
    } 
    return self; 
} 

/* Need to implement the initWithCoder method since this class will be created from the storyboard */ 
-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 

    if (self){ 
     [self setup]; 
    } 
    return self; 
} 

/* Create the UIImageView and add it to the cell's contentView in code. */ 
-(void)setup 
{ 
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)]; 
    [self.contentView addSubview:self.imageView]; 
} 


@end 

И называют эту ячейку из CollectionViewController класса

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

    static NSString *CellIdentifier = @"Photo Cell"; 

    DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor whiteColor]; 
    cell.imageView.image = [UIImage imageNamed:@"Astronaut.jpg"]; 
    // Configure the cell 

    return cell; 
} 

Но у меня есть одна проблема, когда я начинаю мое заявление моя картина показала с малым размером , Что я пропустил?

http://screencast.com/t/ia8tE05P6yc http://screencast.com/t/33N4AhT7

+0

Что делает возвращение self.bounds? Я думаю, что это будет размер ячейки по умолчанию (50 x 50), когда вы проверяете это в методе init. – rdelmar

+0

Я ручной размер ячейки ячейки в main.storyboard 155 x 155. Думаю, я пропустил также установить размер ячейки в настройках Collection View. Я открыл раскадровку в качестве исходного кода и нашел место, где должно быть 155 х 155. – Dens

+0

Dens

ответ

0

cell.imageView, объект ImageView не такой же, как вы, которые добавили в ячейке Interface Builder. UICollectionViewCell имеет размер UIImageView по умолчанию, доступ к которому можно получить, вы можете получить доступ к свойству customView imageView в методе cellForIndexPath метода collectionView, связав imageView subClass DBPhotoCollectionViewCell с конструктором интерфейса, который вам не нужно добавлять [self.contentView addSubview: self.imageView]; в методе настройки.

Второй подход будет присвойте значение тега в ImageView при добавлении в ячейку contentView

/* Create the UIImageView and add it to the cell's contentView in code. */ 
-(void)setup 
{ 
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)]; 
self.imageView.tag=1; 
    [self.contentView addSubview:self.imageView]; 
} 

//And access imagView in cellForIndexPath 

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

    static NSString *CellIdentifier = @"Photo Cell"; 

    DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor whiteColor]; 
    UIImageView *imageView=[cell.contentView viewWithTag:1]; 
    imageView.image = [UIImage imageNamed:@"Astronaut.jpg"]; 
    // Configure the cell 

    return cell; 
} 
+0

Спасибо, это сработало для меня! – Dens

+0

Прохладный !!! Я рад его работе. – Shashi3456643

+0

Можете ли вы проголосовать за меня и отметить как правильный ответ – Shashi3456643