CellForItemAtIndexPath没有响应UICollectionVieLayout

时间:2014-12-14 18:42:28

标签: ios objective-c uicollectionview

我正在处理UICollectionViewUICollectionViewFlowLayout,我的应用目标是在小尺寸和大尺寸的布局中重新调整图像大小,并将UILable放在大尺寸图像视图布局中并使用按钮删除小尺寸布局。

我可以成功重新调整图像布局的大小,但在这种情况下我有1个问题和2个问题。

问题1:

每次都没有调用{p> CellForItemAtIndexPath,我尝试使用reloadData,但它说 快照未呈现的视图会导致空快照。确保您的观点一直如此 在快照之前至少渲染一次。如何解决这个问题并调用此方法。

问题1:

如何在大视图中显示UILabel并在小视图中删除

问题2:

如果我在大图像布局的第4行并转到小视图图像布局并返回大视图布局。如何在大视图布局中显示相同的第4行图像,而不是再次显示刷新的屏幕。

请在下面找到我的代码供您参考:

{//View Controller

#import <UIKit/UIKit.h>

@interface ViewController  : UICollectionViewController<UICollectionViewDataSource, UICollectionViewDelegate>
-(void)animationDidChange:(id)sender;
@end


#import "ViewController.h"
#import "CollectionViewCell.h"
#import "SingleView.h"
#import "GridView.h"

@interface ViewController ()
@property (nonatomic, strong) SingleView *largeLayout;
@property (nonatomic, strong) GridView *smallLayout;
@property (nonatomic, strong) NSArray *images;
@property(nonatomic, strong)UIButton * button;


@end

static NSString *ItemIdentifier = @"ItemIdentifier";

@implementation ViewController

-(void)loadView
{


    self.smallLayout = [[GridView alloc] init];
    self.largeLayout = [[SingleView alloc] init];

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.largeLayout];
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    self.collectionView.bounces = YES;
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    self.automaticallyAdjustsScrollViewInsets=YES;
}


- (void)viewDidLoad {

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame = CGRectMake(0, 18, self.view.frame.size.width, 44);
    toolbar.barTintColor=[UIColor whiteColor];

    //Added Label to toolbar for title
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 23)];
    label2.textAlignment = NSTextAlignmentLeft;
    label2.backgroundColor = [UIColor clearColor];
    label2.textColor = [UIColor grayColor];
    label2.text = @"Title";
    label2.font = [UIFont systemFontOfSize:14.0];
    UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label2];
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                            target:nil    action:nil];
    //Added button to the toolbar
   _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_button addTarget:self
               action:@selector(animationDidChange:)
     forControlEvents:UIControlEventTouchUpInside];
    [_button setBackgroundImage:[UIImage imageNamed:@"on"] forState:UIControlStateNormal];
    _button.backgroundColor=[UIColor clearColor];
    _button.frame = CGRectMake(150.0, 210.0, 40.0, 40.0);
    UIBarButtonItem *toolBarButton = [[UIBarButtonItem alloc] initWithCustomView:_button];
    NSArray *items = [[NSArray alloc] initWithObjects: toolBarTitle,spacer,toolBarButton, nil];
    [toolbar setItems:items];
    self.navigationItem.titleView = toolbar;

  [super viewDidLoad];
}

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

#pragma mark - User Interface Methods

-(void)animationDidChange:(id)sender
{
    CollectionViewCell * cell= [[CollectionViewCell alloc]init];
    if (self.collectionView.collectionViewLayout == self.smallLayout)
    {
[_button setBackgroundImage:[UIImage imageNamed:@"on"] forState:UIControlStateNormal];
        [self.largeLayout invalidateLayout];
        [self.collectionView reloadData];
        [self.collectionView setCollectionViewLayout:self.largeLayout animated:YES];

    }
    else if(self.collectionView.collectionViewLayout == self.largeLayout)
    {
[_button setBackgroundImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
        [self.smallLayout invalidateLayout];

        [self.collectionView setCollectionViewLayout:self.smallLayout animated:YES];
    }
}

#pragma mark - UICollectionView DataSource & Delegate methods

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

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 11;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];

    [cell setImage:[UIImage imageNamed:[NSString stringWithFormat:@"groupA_%ld.jpg", indexPath.row ]]];

        return cell;



}


@end


//COllectionViewCell

#import <UIKit/UIKit.h>

@interface CollectionViewCell : UICollectionViewCell
@property(nonatomic, strong) UILabel * dateString;
@property(nonatomic, strong)UILabel* nameString;
@property(nonatomic, strong)NSString* layout;
-(void)setImage:(UIImage *)image;
-(void)setLabel;
-(void)removeLabel;
@end


#import "CollectionViewCell.h"
@interface  CollectionViewCell()

@property (nonatomic, strong) UIImageView *imageView;


@end
@implementation CollectionViewCell
@synthesize dateString,nameString,layout;
- (id)initWithFrame:(CGRect)frame
{

    if (!(self = [super initWithFrame:frame])) return nil;

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame)), 0, 0)];

    self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
     self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    if ([self.layout isEqualToString: @"small"]) {
        NSLog(@"Hello");
    }
    self.dateString =[[UILabel alloc]initWithFrame:CGRectMake(0, 270, 350, 20)];
   self.nameString =[[UILabel alloc]initWithFrame:CGRectMake(0, 290, 350, 20)];


    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.dateString];
    [self.contentView addSubview:self.nameString];
    self.backgroundColor = [UIColor whiteColor];

    return self;
}



-(void)setImage:(UIImage *)image
{

    self.imageView.image = image;
}

-(void)setLabel{

self.dateString.text =@"Hello";

}
-(void)removeLabel{
    [self.dateString removeFromSuperview];
}

@end


//CollectionView layout Large

@interface SingleView : UICollectionViewFlowLayout

@end

#import "SingleView.h"

@implementation SingleView
-(id)init
{
    if (!(self = [super init])) return nil;

    self.itemSize = CGSizeMake(350, 300);
    self.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    self.minimumInteritemSpacing = 10.0f;
    self.minimumLineSpacing = 10.0f;


    return self;
}
@end

//CollectionViewLayout Small


#import <UIKit/UIKit.h>

@interface GridView : UICollectionViewFlowLayout

@end


#import "GridView.h"

@implementation GridView
-(id)init
{
    if (!(self = [super init])) return nil;

    self.itemSize = CGSizeMake(130, 130);
    self.sectionInset = UIEdgeInsetsMake(10, 40, 10, 40);
    self.minimumInteritemSpacing = 10.0f;
    self.minimumLineSpacing = 10.0f;
    return self;
}

@end

}

1 个答案:

答案 0 :(得分:0)

很遗憾,我没有直接回答您的问题。当我重新调整设备旋转视图时,我有类似的东西,它通过移动方法消失了 'viewDidLayoutSubviews`

-(void)viewDidLayoutSubviews
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
        [self landscapeUISetup];

    } else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self portraitUISetup];
        NSLog(@"portrait");

    }

}

对于问题1,我建议在加载小视图时尝试简单隐藏标签。 像

这样的东西
self.label.hidden = YES;

当你改回大的时候

self.label.hidden = NO;

问题2: 您需要保存可见单元格的索引,然后调用滚动方法

[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];