我有一個UICollectionViewController與自定義UICollectionViewCell與圖像和標籤插座。只有按下重新加載才能重新加載數據後,圖像才能正確填充。我注意到標籤也消失了,儘管我覺得它們只是在圖像後面。圖像加載不正確,除非reloadData被調用
我在做什麼錯?
ON LOAD
AFTER重裝DATA
這是視圖被加載的圖像時,應爲多大。
我設定的細胞中的屏幕尺寸是多大的一個因素。 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
'cell.photoView.frame = CGRectMake (0,0,cell.bounds.size.width,cell.bounds.size.height);' - 第一次創建的單元格的大小爲0,0。使用自動佈局來保持imageView的大小適合單元格。 – ChrisH
如果我刪除該行,重新加載圖像不會調整大小 –
是的,你需要使用自動佈局設置框架。 – ChrisH