Satheeshkumar Naidu Satheeshkumar Naidu - 4 months ago 23
Objective-C Question

How to reverse the collection view cell

i have an array with 15 elements and I am adding them to collection view cell

_imagearray=[@[@"bootimages",@"fall-photography-in-hocking-hills", @"gift",@"hillimages",@"Mercedes-classe-S-W116_large_dettaglio_articolo",@"prinimages",@"resplendent",@"tnb4",@"Tomato-plant",@"Vole",@"waterimages",@"fall-photography-in-hocking-hills", @"gift",@"hillimages",@"Mercedes-classe-S-W116_large_dettaglio_articolo"]mutableCopy];

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

//[self.collection reloadData];
homeceeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

UIImage *image;
NSInteger row = [indexPath row];


NSLog(@"indexpath = %ld", (long)row);

image = [UIImage imageNamed:_imagearray[row]];

cell.img.image = image;

cell.text.text=[_tittlearray objectAtIndex:indexPath.row];

return cell;
}


and I in the next view controller I am taking an image using camera and saving in to the array and I am passing the array back to the first view controller using
NSNotificationCenter


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedArray:) name:@"ShareArray" object:nil];

-(void) receivedArray:(NSNotification*)notification
{

NSMutableArray* userInfo = notification.object;
UIImage *image = [userInfo firstObject];

[_imagearray addObject:[userInfo firstObject]];

NSLog(@"%@",_imagearray);
[self.collection reloadData];
_collection.delegate=self;
_collection.dataSource=self;


NSLog(@"%@",_imagearray);
}
I am Reloading the collection view cell and the new image doesn't have name
if (indexPath.row == 15) {
image= _imagearray[row];

//[_imagearray insertObject:image atIndex:0];

}else {
image = [UIImage imageNamed:_imagearray[row]];


}
cell.img.image = image;

cell.text.text=[_tittlearray objectAtIndex:indexPath.row];
}


I want to add new image at the top but it is in the bottom

please help me out i am new to the objective c

Answer

Check my answer@ satheesKumar Naidu

imagearray=[@[@"bootimages",@"fall-photography-in-hocking-hills", @"gift",@"hillimages",@"Mercedes-classe-S-W116_large_dettaglio_articolo",@"prinimages",@"resplendent",@"tnb4",@"Tomato-plant",@"Vole",@"waterimages",@"fall-photography-in-hocking-hills", @"gift",@"hillimages",@"Mercedes-classe-S-W116_large_dettaglio_articolo"]mutableCopy];

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

     //[self.collection reloadData];
    homeceeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    UIImage *image;
    NSInteger row = [indexPath row];


    NSLog(@"indexpath = %ld", (long)row);
   // here i have make the changes** 
    if( [_imagearray[row] isKindOfClass:[UIImage class]]){
       image = _imagearray[row]
    }
   else {
        image = [UIImage imageNamed:_imagearray[row]];
    }

    cell.img.image = image;

    cell.text.text=[_tittlearray objectAtIndex:indexPath.row];

        return cell;
}


 -(void) receivedArray:(NSNotification*)notification {

    NSMutableArray* userInfo = notification.object;
   if([userInfo count]>0) {
    UIImage *image = [userInfo firstObject];
    ///[_imagearray addObject:[userInfo firstObject]];  use this it will diretly add to end of the last index
    [_imagearray insertObject:[userInfo firstObject] atIndex:0];  // it will add to firstindex of the array value
    NSLog(@"%@",_imagearray);
    [self.collection reloadData];
     // here do not want add the delegate itself.
    NSLog(@"%@",_imagearray);
    }  // it will check the nil object and to prevent the crash
 }