Ios UICollectionView未正确重新加载数据

Ios UICollectionView未正确重新加载数据,ios,objective-c,xcode,uiview,uicollectionview,Ios,Objective C,Xcode,Uiview,Uicollectionview,我正在使用UICollectionView呈现一些图像。当按下图像时,我希望它高亮显示图像。我创建了一个自定义类ImageSelectionViewController,该类为给定的图像数组创建uicollectionview并响应UICollectionViews委托 单元格的大小取决于提供的图像是纵向还是横向 我有一个名为_ArrayOfBoolsof NSNumber的数组,用于存储布尔值,其中一个在ImageSelectionViewController中高亮显示,另一个在ImageSe

我正在使用UICollectionView呈现一些图像。当按下图像时,我希望它高亮显示图像。我创建了一个自定义类ImageSelectionViewController,该类为给定的图像数组创建uicollectionview并响应UICollectionViews委托

单元格的大小取决于提供的图像是纵向还是横向

我有一个名为_ArrayOfBoolsof NSNumber的数组,用于存储布尔值,其中一个在ImageSelectionViewController中高亮显示,另一个在ImageSelectionViewController中不高亮显示,该控制器保存需要高亮显示的图像的存储。当按下图像时,它会更新arrayOfBools,然后我重新加载UICollectionView以更新所有单元格

该应用程序加载良好,但只要我点击图像两次,就会出现奇怪的格式问题,图像大小不正确。我的项目在这里。当我为UICollectionView重新加载数据时,单元格的大小几乎发生了变化

这是我的ImageViewController.h

//
//  ImageSelectionViewController.h
//  QUESTIONS
//
//  Created by Kingsnorth, Alec (Student) on 15/01/2015.
//  Copyright (c) 2015 Kingsnorth, Alec (Student). All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CustomCollectionViewCell.h"

typedef enum imageSelectMode{ICMOne, ICMMulti, ICMZeroOrOne, ICMZeroOrMulti}imageSelectMode;



@interface ImageSelectionViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray* arrayImages;
    NSMutableArray* imageSizes;
    // dimensions and spacings
    float collectionViewWidth;
    float collectionViewHeight;

    float screenWidth;
    float screenHeight;

    float landscapeHeight;
    float portraitHeight;
    imageSelectMode imSelectMode;
    NSString* test;

    //BOOL shouldHighlight;

}
//where the data is kept
@property(nonatomic,retain) NSMutableArray* arrayOfBools;
@property(nonatomic,retain) UICollectionView* collectionView;

-(UIView*)setupWithImages:(NSMutableArray*)imageArray imageSelectMode:(imageSelectMode)mode;

@end

如前所述,不清楚你的问题是什么。你没有提供足够的代码,因为我们无法从你在这里写的内容中复制你的问题,很少有人会点击该链接来查看你的项目-你的问题应该是独立的,不依赖外部链接。如果你能更具体地描述你的问题,而不是简单地说一句奇怪的话,那会有所帮助出现格式化问题。
//
//  ImageSelectionViewController.m
//  QUESTIONS
//
//  Created by Kingsnorth, Alec (Student) on 15/01/2015.
//  Copyright (c) 2015 Kingsnorth, Alec (Student). All rights reserved.
//

#import "ImageSelectionViewController.h"

@interface ImageSelectionViewController ()

@end

@implementation ImageSelectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

-(UIView*)setupWithImages:(NSMutableArray*)imageArray imageSelectMode:(imageSelectMode)mode;
{
    //House keeping
    [self setupDimensions];
    _arrayOfBools=[[NSMutableArray alloc] init];
    imSelectMode=mode;
    for (int i=0; i<[imageArray count]; i++)
    {
        [_arrayOfBools addObject:[NSNumber numberWithBool:NO]];
    }

    arrayImages=imageArray;


    //setupCollectionView
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
   _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, collectionViewWidth, collectionViewHeight)collectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    [_collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"CUSTOM"];
    [_collectionView setBackgroundColor:[UIColor whiteColor]];

    return _collectionView;
}

-(void)setupDimensions
{
    screenWidth=768;
    screenHeight=1026;

    collectionViewWidth=screenWidth-20;
    collectionViewHeight=900;

    landscapeHeight=240;
    portraitHeight=290;

    test=[[NSString alloc] init];



}



//Delegate methods----------------------------------//
//----------------------------------------//
//---------------------//
//----//


- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{

    return [arrayImages count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CUSTOM" forIndexPath:indexPath];

    //NSLog(@"\n arrayOfBools: \n %@",self.arrayOfBools);

    [cell updateImage:[arrayImages objectAtIndex:indexPath.item] withHighlight:[[_arrayOfBools objectAtIndex:indexPath.item] boolValue]andItemNo:indexPath.item];

    //NSLog(@"\nrowNo:%ld",(long)indexPath.item);

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{


    //  Choose which mode will affect the highlight properties
    if (imSelectMode==ICMOne)
    {

        for (int i=0; i<[self.arrayOfBools count]; i++)
        {
            [self.arrayOfBools replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:FALSE]];
        }

        [self.arrayOfBools replaceObjectAtIndex:indexPath.item withObject:[NSNumber numberWithBool:YES]];
    }
    else if (imSelectMode==ICMMulti)
    {
        NSNumber* num=[self.arrayOfBools objectAtIndex:indexPath.item];
        NSNumber* replace;
        if (num.boolValue==YES)
        {
            replace=[NSNumber numberWithBool:FALSE];

        }
        else
        {
            replace=[NSNumber numberWithBool:TRUE];
        }

        [self.arrayOfBools replaceObjectAtIndex:indexPath.item withObject:replace];

    }
    else if (imSelectMode==ICMZeroOrOne)
    {
        for (int i=0; i<[self.arrayOfBools count]; i++)
        {
            if (i==indexPath.item)
            {
                //Do nothing
            }
            else
            {
                [self.arrayOfBools replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:FALSE]];
            }
        }

        NSNumber* num=[self.arrayOfBools objectAtIndex:indexPath.item];
        NSNumber* replace;
        if (num.boolValue==YES)
        {
            replace=[NSNumber numberWithBool:FALSE];

        }
        else
        {
            replace=[NSNumber numberWithBool:TRUE];
        }

        [self.arrayOfBools replaceObjectAtIndex:indexPath.item withObject:replace];
    }




    [self updateCollectionView:collectionView];
}

-(void)updateCollectionView:(UICollectionView*)collectionView
{
      NSLog(test);

    [collectionView reloadItemsAtIndexPaths:[collectionView indexPathsForVisibleItems]];
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    UIImage* currImage=[arrayImages objectAtIndex:indexPath.item];

    //find the aspect ratio of the image w1/h1 = w2/h2

    float w1=currImage.size.width;
    float h1=currImage.size.height;

    float ratio=w1/h1;
    float h2=0;
    float w2=0;

    //Resize image to standard sizes
    // resize image locking aspect


    if (currImage.size.width>currImage.size.height)
    {    // image is landscape
         h2=landscapeHeight;
    }
    else
    {
        //Image is portrait
         h2=portraitHeight;
    }


    w2=ratio*h2;

    CGSize toSend;
    toSend=CGSizeMake(w2,h2);

    //NSString* log=[NSString stringWithFormat:@"\n Index:%ld width:%f height:%f ",(long)indexPath.item,w2,h2];
    //test=[test stringByAppendingString:log];

    printf([NSString stringWithFormat:@"\n indexPath.item %lu",indexPath.item].UTF8String);

    return toSend;

}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
@end
//
//  CustomCollectionViewCell.h
//  QUESTIONS
//
//  Created by Kingsnorth, Alec (Student) on 15/01/2015.
//  Copyright (c) 2015 Kingsnorth, Alec (Student). All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomCollectionViewCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImage *image;
-(void)updateImage:(UIImage*)image withHighlight:(BOOL)isHighlighted andItemNo:(NSInteger)item;

@end
//
//  CustomCollectionViewCell.m
//  QUESTIONS
//
//  Created by Kingsnorth, Alec (Student) on 15/01/2015.
//  Copyright (c) 2015 Kingsnorth, Alec (Student). All rights reserved.
//

#import "CustomCollectionViewCell.h"

@implementation CustomCollectionViewCell

-(void)updateImage:(UIImage*)image withHighlight:(BOOL)isHighlighted andItemNo:(NSInteger)item
{
    for (UIView* subview in     [self.contentView subviews])
    {
        [subview removeFromSuperview];
    }

    // NSLog(@"%d",isHighlighted);
    //Image with
    self.image=image;
    self.imageView=[[UIImageView alloc] initWithFrame:self.contentView.frame];

    //NSLog(@"bounds: ",NSStringFromCGRect(self.contentView.frame));
    self.imageView.image=image;
    self.imageView.hidden=NO;


    if (isHighlighted==YES)
    {
        [_imageView.layer setBorderColor: [[UIColor redColor] CGColor]];
        [_imageView.layer setBorderWidth: 2.0];

    }
    else
    {

    }

    UILabel* label=[[UILabel alloc]initWithFrame:self.contentView.bounds];
    label.text=[NSString stringWithFormat:@"%ld",(long)item];

    self.imageView.bounds=self.contentView.bounds;

    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:label];

}


@end