2015-09-24 47 views
0

我有一個UICollectionViewController與自定義UICollectionViewCell與圖像和標籤插座。只有按下重新加載才能重新加載數據後,圖像才能正確填充。我注意到標籤也消失了,儘管我覺得它們只是在圖像後面。圖像加載不正確,除非reloadData被調用

我在做什麼錯?

ON LOAD

enter image description here

AFTER重裝DATA

這是視圖被加載的圖像時,應爲多大。

enter image description here

我設定的細胞中的屏幕尺寸是多大的一個因素。 NSLogging框架我可以看到單元格大小和圖像大小是相同的。

Cell size: 124.85 
PhotoView size: {{0, 0}, {124.85, 124.85}} 

CollectionViewController.h

#import <UIKit/UIKit.h> 
#import "HomeModel.h" 
@interface CollectionViewController : UICollectionViewController <UICollectionViewDelegateFlowLayout> 
@end 

CollectionViewController.m

#import "CollectionViewController.h" 
#import "CollectionViewCell.h" 


#define SPACE_BETWEEN_CELLS 0.05 

@interface CollectionViewController() { 

    NSArray * _feedItems; 
} 
- (IBAction)reloadUserData:(id)sender; 

@end 

@implementation CollectionViewController 

static NSString * const reuseIdentifier = @"Cell"; 


- (IBAction)reloadUserData:(id)sender { 
    [self.collectionView reloadData]; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations 
    self.clearsSelectionOnViewWillAppear = YES; 
    self.collectionView.allowsMultipleSelection = NO; 

    _feedItems = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", nil]; 


} 

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


#pragma mark <UICollectionViewDataSource> 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return [_feedItems count]; 
} 

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

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 


    //IMAGE 
    cell.photoView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height); 
    cell.photoView.backgroundColor = [UIColor colorWithRed:128 green:128 blue:128 alpha:0.5]; 

    cell.photoView.image = [UIImage imageNamed:[_feedItems objectAtIndex:indexPath.row]]; 

    NSLog(@"PhotoView size: %@", NSStringFromCGRect(cell.photoView.frame)); 




    return cell; 
} 



#pragma mark <UICollectionViewDelegateFlowLayout> 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    CGFloat size = ([[UIScreen mainScreen] bounds].size.width/3) - (SPACE_BETWEEN_CELLS * 3); 

    NSLog(@"Cell size: %f ", size); 

    return CGSizeMake(size, size); 
} 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 

    return SPACE_BETWEEN_CELLS; 
} 



@end 

這裏是我的自定義單元格

CollectionViewCell.h

#import <UIKit/UIKit.h> 
@interface CollectionViewCell : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *photoView; 
@property (weak, nonatomic) IBOutlet UILabel *textLabel; 
@end 

CollectionViewCell.m

#import "CollectionViewCell.h" 

@implementation CollectionViewCell 

- (void)awakeFromNib {} 

@end 
+0

'cell.photoView.frame = CGRectMake (0,0,cell.bounds.size.width,cell.bounds.size.height);' - 第一次創建的單元格的大小爲0,0。使用自動佈局來保持imageView的大小適合單元格。 – ChrisH

+0

如果我刪除該行,重新加載圖像不會調整大小 –

+0

是的,你需要使用自動佈局設置框架。 – ChrisH

回答

0

縮小圖像視圖(圖片視圖)的高度, 不要將其高度相同單元格的高度,否則該標籤也將消失。

cell.photoView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height - 20);

如果使用自動佈局給圖像視圖,約束從(頂部,左側和右側,並設置其高度),併爲標籤給約束條件(底部和水平對齊中心在細胞)。

如果使用自動調整大小然後執行以下的設定,

用於圖像視圖

enter image description here

和用於標籤

enter image description here

+1

問題的一部分是AutoLayout。雖然該標籤沒有被推出,但它隱藏在圖像背後。謝謝你的幫助。 AutoLayout有時是一場噩夢。 –