2016-08-02 4 views
0

У меня есть массив из 15 элементов, и я добавляю их в коллекцию вида клеткуКак реверс ячейки просмотра коллекции

_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; 
} 

и я в следующем контроллере представления я беру изображение с помощью камеры и сохранения в в массив и я передаю массив обратно в первый контроллер представления с использованием 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]; 
} 

Я хочу, чтобы добавить новое изображение на вершине, но он находится в нижней

, пожалуйста, помогите мне я новичок в Objective C

+0

просто добавить изображение в месте в «0» индекса и перезагрузите CollectionView [_imagearray insertObject: @ "imagename" atIndex: 0]; –

+0

использование [_imagearray insertObject: [userInfo firstObject] atIndex: 0]; in receivedArray метод –

+0

проверить мой ответ @satheeskumar naidu. если у вас есть сомнения, спросите меня –

ответ

0

Проверьте мой ответ @ satheesKumar Найду

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 
} 
+0

благодарит за вас быстрый ответ, основная проблема - это массив userInfo, но у него есть имя –

+0

, но в пути коллекции cellforRowindex. вы осуществили с imagename, чтобы показать изображение в коллекции. –

+0

UIImage * image; NSInteger row = [indexPath row]; NSLog (@ "indexpath =% ld", (длинная) строка); 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]; возвратная ячейка; пожалуйста, пройдите код –

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

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

      //[_imagearray addObject:[userInfo firstObject]]; 
     if(image){ 
     [_imagearray insertObject:image atIndex:0]; // it will insert element at 0 index and will show first place in collection view 

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


     NSLog(@"%@",_imagearray); 
     } 
} 
+0

Также установите флажок, чтобы убедиться, что [userInfo firstObject] не равен нулю, прежде чем вставлять элемент в Array, иначе он сработает. –

Смежные вопросы