Not refresh UI in the collection view

Hi,

I have this code:

@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tabBarController setSelectedIndex:0];

self.collectionView.dataSource = self;
self.collectionView.delegate = self;

//cell
[self.collectionView registerNib:[CarblueHorizontalCollectionCell nib] forCellWithReuseIdentifier:@"CarblueHorizontalCollectionCell"];
[self.collectionView registerNib:[CarredHorizontalCollectionCell nib] forCellWithReuseIdentifier:@"CarredHorizontalCollectionCell"];
[self.collectionView registerNib:[CarblackHorizontalCollectionCell nib] forCellWithReuseIdentifier:@"CarblackHorizontalCollectionCell"];

[self loadRecentAll];

[self setScrollStartOffsetY:0];

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
[self.tabBarController setSelectedIndex:0];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self loadRecentAll];

}

-(void)loadRecentAll{

self.arrCarBlue = [Helper loadFavoriteCarBlueObjects];
if(self.arrCarBlue.count!=0 || self.arrCarBluet !=nil || ![self.arrCarBlue  isKindOfClass:[NSNull class]]) {
    
    NSArray* reversedArray = [[self.arrCarBlue  reverseObjectEnumerator] allObjects];
    self.arrCarBlue =reversedArray;
    [self.collectionView reloadData];
}

}

But, the data is not updated in the collection view. Indeed, the data is updated correctly, but the UI no. In fact, if I click on a cell, in the debug, I see the correct data but I do not see the correct data as a graphical interface. Why? In attached, I uploaded my helper file.

Thanks! :grin:

myHelper.zip (1.6 KB)

Then,

Here are Cell files. File.h:

#import "CollectionViewCell.h"
#import "CarblueGroupObject.h"

@protocol CarblueHorizontalCollectionCellDelegate <NSObject>

-(void)didSelectCarblueHorizontalCellAtSection:(NSInteger)section IndexPath:(NSIndexPath *) indexpath;

@end

 @interface CarblueHorizontalCollectionCell : CollectionViewCell <UICollectionViewDelegate , UICollectionViewDataSource>

@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic, weak) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *locationArray;
@property (nonatomic, assign) NSInteger section;
@property (nonatomic, assign) id <CarblueHorizontalCollectionCellDelegate> delegate;

- (void) setCarblueGroupObjet:(CarblueGroupObject*)objGroup displayGroupName:(BOOL)show;

file .m:

@implementation CarblueHorizontalCollectionCell

- (void)awakeFromNib {
// Initialization code
[super awakeFromNib];

self.arrCarblue = [Helper loadFavoriteCarblueObjects];
NSArray* reversedArray = [[self.arrCarblue reverseObjectEnumerator] allObjects];
self.arrCarblue=reversedArray;

[self.collectionView registerNib:[CarblueCollectionViewCell nib] forCellWithReuseIdentifier:@"CarblueCollectionViewCell"];

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"goCarblue"
                                              object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(openCarblue:) name:@"goCarblue" object:nil];

}

 -(void)viewDidLayoutSubviews {
self.flowLayout.itemSize =CGSizeMake(200, 65);

}

- (void) setCarblueGroupObjet:(CarblueGroupObject*)objGroup displayGroupName:(BOOL)show
{
self.objCarblueGroup = objGroup;
self.willShowGroupName = show;

}

 #pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(200, 65);

}

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

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

if(_arrCarblue.count==1){
    return 1;
}else if(_arrCarblue.count ==2){
    return 2;
}else if(_arrCarblue.count ==3){
    return 3;
}else if(_arrCarblue.count ==4){
    return 4;
}else if(_arrCarblue.count ==5){
    return 5;
}else{
    return 5;
}
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *cellIdentifier = @"CarblueCollectionViewCell";
CarblueCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

CarblueObject *obj;
obj=[_arrCarblue objectAtIndex:indexPath.row];

cell.lblCar.text=[NSString stringWithFormat:@"%@",obj.Car];

self.collectionView.scrollsToTop = NO;

return cell;

}

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(didSelectCarblueHorizontalCellAtSection:IndexPath:)]) {
    [self.delegate didSelectCarblueHorizontalCellAtSection:self.section IndexPath:indexPath];
}

}

 #pragma mark - Refresh Collection

-(void)openCarblue:(NSNotification *)notification
{
if ([[notification name] isEqualToString:@"goCarblue"])
{
    [self refresh_collection];
}

}

-(void)refresh_collection{

self.arrCarblue = [Helper loadFavoriteCarblueObjects];
NSArray* reversedArray = [[self.arrCarblue reverseObjectEnumerator] allObjects];
self.arrCarblue=reversedArray;
//self.collectionView.scrollsToTop=YES;
[UIScrollView beginAnimations:@"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:1.0f];
[self.collectionView setContentOffset:CGPointMake(0, 0)];
[UIScrollView commitAnimations];
[self.collectionView reloadData];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

}

The strange thing is that if I step from one view to the other, the refresh does not work. But, if I close the app and I open the App again, the refresh works. Why?

Some advice? Thanks!

Does anyone have any idea? A help? I can not solve it.