UICollectionView无法正确重新加载数据

时间:2015-01-21 16:08:30

标签: ios objective-c xcode uiview uicollectionview

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

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

我有一个名为 _arrayOfBools 的NSNumbers数组,它存储布尔值,(One)在ImageSelectionViewController内部突出显示,0(未突出显示),用于存储需要突出显示的图像。按下图像时,它会更新arrayOfBools然后重新加载我的UICollectionView以更新所有单元格。

该应用加载得很好,但只要我点击图像两次,就会出现奇怪的格式问题,而图像尺寸不正确。我的项目在XcodeProject。这几乎就像当我为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

这是我的ImageViewController.m

//
//  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

//
//  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

//
//  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

0 个答案:

没有答案