除非调用reloadData,否则图像无法正确加载

时间:2015-09-24 18:39:01

标签: ios objective-c uiview uiviewcontroller uicollectionview

我有一个带有图像和标签插座的自定义UICollectionViewCell的UICollectionViewController。在我按下重新加载以重新加载数据后,图像只能正确填充。我注意到标签也消失了,虽然我觉得它们 就在图像的后面。

我做错了什么?

ON LOAD

enter image description here

重新加载数据

这是加载视图时图像的大小。

enter image description here

我将细胞设置为屏幕尺寸有多大的因素。 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

1 个答案:

答案 0 :(得分:0)

降低图像视图的高度(照片视图), 不要保持其高度与单元格的高度相同,否则标签也会消失。

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

如果使用自动布局给出图像视图,来自(顶部,左侧和右侧并设置其高度)的约束以及标签给出约束(底部并在单元格中水平居中对齐)。

如果使用自动调整大小,请设置以下设置

用于图像视图

enter image description here

和标签

enter image description here