我有一个包含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];
}
我想在顶部添加新图像,但在底部
请帮助我,我对目标c还是陌生的
检查我的答案@ sathees
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句