我有一个带有图像和标签插座的自定义UICollectionViewCell的UICollectionViewController。在我按下重新加载以重新加载数据后,图像只能正确填充。我注意到标签也消失了,虽然我觉得它们 就在图像的后面。
我做错了什么?
ON LOAD
重新加载数据
这是加载视图时图像的大小。
我将细胞设置为屏幕尺寸有多大的因素。 NSLogging the Frames我可以看到Cell的大小和图像大小是一样的。
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