【问题标题】:Images do not load correctly unless reloadData is called除非调用 reloadData,否则图像无法正确加载
【发布时间】:2015-09-24 18:39:01
【问题描述】:

我有一个带有自定义 UICollectionViewCell 的 UICollectionViewController 和一个图像和一个标签出口。只有在我按下重新加载以重新加载数据后,图像才会正确填充。我注意到标签也消失了,尽管我觉得它们 就在图片后面。

我做错了什么?

加载中

重新加载数据后

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

我将单元格设置为屏幕大小的一个因素。 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

【问题讨论】:

  • cell.photoView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height); - 第一次创建单元格时,帧大小为 0,0。使用自动布局将 imageView 的大小保持为单元格。
  • 如果我删除该行,重新加载图像时不会调整大小
  • 是的,您需要使用自动布局来设置框架。

标签: ios objective-c uiview uiviewcontroller uicollectionview


【解决方案1】:

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

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

如果使用自动布局,则为图像视图提供约束(顶部、左侧和右侧并设置其高度),并为标签提供约束(底部并在单元格中水平居中对齐)。

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

用于图像视图

对于标签

【讨论】:

  • 部分问题是 AutoLayout。虽然标签没有被推到视野之外,但它隐藏在图像后面。谢谢你的帮助。 AutoLayout 有时是一场噩梦。
猜你喜欢
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
相关资源
最近更新 更多