In my application,I am using Collection view as i'd take in Xib.now there is image of array come from server side. And they are correctly seen in both iphone 3.5 and 4 inch screen.But when i select device as ipad it seem like this:
There are three rows appear and i don't know how to solve this problem.I want only one row which will be scrollable.
Here is my code:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imagArr count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
QBAssetsCollectionViewCell *cell = (QBAssetsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"QBAssetsCollectionViewCell" forIndexPath:indexPath];
NSDictionary *imageName = [[imagArr objectAtIndex:indexPath.row] objectForKey:#"Content"];
NSString *sr = [imageName objectForKey:#"text"];
cell.Img.image = [self decodeBase64ToImage:sr];
//[cell updateCell];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%ld",(long)indexPath.row);
NSDictionary *imageName = [[imagArr objectAtIndex:indexPath.row] objectForKey:#"Content"];
NSString *sr = [imageName objectForKey:#"text"];
cloud.image= [self decodeBase64ToImage:sr];
[self.collectionView setHidden:YES];
}
Related
I want to display random image from array in my UICollectionView. I work with xib, and already create custom cell with image view. I want that when I click my button the image displaying in collection view, but don't know how to do this. I have 2 array in tempArr I initialize images, and in arrayForImages I insert image when button press.
Here is my code:
- (IBAction)randomButton:(id)sender {
NSInteger randIndex = arc4random() % [tempArr count];
NSInteger ind = arrayForImages.count;
[arrayForImages insertObject:tempArr[randIndex] atIndex:ind];
NSLog(#"array for index %#", arrayForImages);
}
Here is the Collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return arrayForImages.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, 100);
}
- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.imageViewCell.image = [UIImage imageNamed:[arrayForImages objectAtIndex:indexPath.row]];
return cell;
}
I'm populating data on uicollectionview cell and selecting and deselecting, everything works perfect but when I start scrolling sometime selection is not there sometimes selection changing with the cell. Below is the code, help much appreciated.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell = (BYOCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"CVCCell" forIndexPath:indexPath];
cell.vSelectionView.hidden = YES;
cell.vSelectionView.backgroundColor = customLightGreenColor;
[self makeRoundElement:cell.vSelectionView forLabel:nil withCorner:8.0f withBorder:0];
pizzaInfo *pizzainfo= [[pizzaInfo alloc]init];
pizzainfo = [_lstDishCollection objectAtIndex: indexPath.row];
if (pizzainfo._bIsSelected)
{
cell.vSelectionView.hidden = NO;
}
else
{
cell.vSelectionView.hidden = YES;
}
//label customization
return cell;
}
DidselectItem
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = (BYOCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"CVCCell" forIndexPath:indexPath];
pizzaInfo *pizzaInfoCellData = [_lstDishCollection objectAtIndex: indexPath.row];
byoPizzaInfo = [_lstDishCollection objectAtIndex:indexPath.row];
if ( pizzaInfoCellData._bIsSelected)
{
cell.vSelectionView.hidden = NO;
pizzaInfoCellData._bIsSelected = NO;
[self._byodelegate deltaDeSelection:pizzaInfoCellData];
}
else
{
cell.vSelectionView.hidden = YES;
pizzaInfoCellData._bIsSelected = YES;
// deltaSelection:(pizzaInfo *)selectedItem
[self._byodelegate deltaSelection:pizzaInfoCellData];
if (self._IsNotifiable) {
[self showView];
}
}
[_vCVC reloadData];
}
More over collectionViewCell is inside tableViewCell.
In your cellForItemAtIndexPath you have already added condition for hide and show the selected view so you need to change your didSelectItemAtIndexPath like this
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
pizzaInfo *pizzaInfoCellData = [_lstDishCollection objectAtIndex: indexPath.row];
if (pizzaInfoCellData._bIsSelected)
{
[self._byodelegate deltaDeSelection:pizzaInfoCellData];
}
else
{
[self._byodelegate deltaSelection:pizzaInfoCellData];
}
pizzaInfoCellData._bIsSelected = !pizzaInfoCellData._bIsSelected
[_vCVC reloadData];
}
Note:- Class name always start with uppercase latter so it is batter if you change your class name pizzaInfo with PizzaInfo, its suggestion for good coding guidelines.
I'm created a collection view of sticker like Facebook Message and add auto layout for them in storyboard. Its ok on iOS 8. But when I'm run its on iOS 7 all items in collection view are disappeared. Can someone tell me what's wrong with my code and how to solve this problem?
Here is my code:
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return stampCategorySelected.templetes.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
StampsCollectionViewCell *cell = (StampsCollectionViewCell*) [collectionView dequeueReusableCellWithReuseIdentifier:kStampCellIdentifier forIndexPath:indexPath];
NSString *imgPath = [NSString stringWithFormat:#"%#/%#", STAMP_PATH, stampCategorySelected.templetes[indexPath.row]];
cell.stamp.image = [UIImage imageNamed:imgPath];
//Set page control count
int pageCount = floor(collectionView.contentSize.width / collectionView.frame.size.width) + 1;
_pagesControl.numberOfPages = pageCount;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(PC_Width/4, PC_Width/4);
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
_clipart.kind = #"stamp";
_clipart.clipartid = [stampCategorySelected.templetes[indexPath.row] stringByDeletingPathExtension];
_clipart.clipartPath = stampCategorySelected.templetes[indexPath.row];
_clipart.index = 0;
_clipart.center = [Utilities arrayFromPoint:DEFAULT_CENTER];
UIImage * clipartImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#", STAMP_PATH, stampCategorySelected.templetes[indexPath.row]]];
if (!clipartImage) {
clipartImage = [UIImage imageNamed:[STAMP_PATH stringByAppendingPathComponent:stampCategorySelected.templetes[indexPath.row]]];
}
_clipart.size = #[#(_templateSize.width/5), #((clipartImage.size.height/clipartImage.size.width)*_templateSize.width/5)];
_clipart.maxscale = 1;
_clipart.scale = 1.0f;
_clipart.angle = 0.0f;
FSLog(#"Selected clipart: %#", _clipart);
[_delegate stampViewController:self didSelectStamp:_clipart];
[self dismiss:nil];
}
I am trying to select a cell in UICollectionView, it gets selected but on scroll down it selects the some other cell on the bottom and scroll up it shows some other to be selected.
Below is the code which I am using didSelectItemAtIndexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *newIndex = indexPath;
cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];
NSString *strId = [[masterArray valueForKey:#"id"]objectAtIndex:indexPath.row];
NSString *tempIndexRow = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
NSLog(#"%#, %#,%d ,%#, %d", strId,tempIndexRow,cell.imageView.tag,[boolArray objectAtIndex:indexPath.row],indexPath.row);
if (strId && [[boolArray objectAtIndex:indexPath.row] isEqualToString:#"False"] && cell.imageView.tag == indexPath.row) {
cell.selectedImage.image = [UIImage imageNamed:#"select.png"];
[boolArray replaceObjectAtIndex:indexPath.row withObject:#"True"];
}
else{
cell.selectedImage.image = Nil;
[boolArray replaceObjectAtIndex:indexPath.row withObject:#"False"];
}
}
This is what I select for the first time
This is what I get when I scroll down
Thanks
You need to set selected item index store in to one array and at cellForRowIndex time check this array index with indexPath.row like bellow:-
selectedCellsArray alloc this array at ViewDIdLoad method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];
if ( [selectedCellsArray containsObject:[NSString stringWithFormat:#"%d",indexPath.row]] )
{
[selectedCellsArray removeObject:[NSString stringWithFormat:#"%d",indexPath.row]];
cell.selectedImage.image = Nil;
}
else
{
[selectedCellsArray addObject:[NSString stringWithFormat:#"%d",indexPath.row]];
cell.selectedImage.image = [UIImage imageNamed:#"select.png"];
}
}
and
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCellsArray containsObject:[NSString stringWithFormat:#"%#",rowNsNum]] )
{
cell.selectedImage.image = [UIImage imageNamed:#"select.png"];
}
else
{
cell.selectedImage.image = Nil;
}
}
It is not selecting another cell. Problem is mostlikely due to you reusing cells and not reintializing them correctly when they come back on the screen. When Cells not visible, it unloaded to save memory.
So better check this condtion as well in cellForItemAtIndexPath datasource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//You cell initialization code.
if ([[boolArray objectAtIndex:indexPath.row] isEqualToString:#"True"] ) {
cell.selectedImage.image = [UIImage imageNamed:#"select.png"];
}
else{
cell.selectedImage.image = Nil;
}
}
I have a Navigation Controller pushing to a Collection View Controller. This collectionView has an NSArray of images, and I'm sure there are some elements in the collectionView using the method visibleCells.
But when I launch it there is no displaying cells.
Here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
images = [NSArray arrayWithObjects:#"angry_birds_cake.jpg", #"creme_brelee.jpg", #"egg_benedict.jpg", #"full_breakfast.jpg", #"green_tea.jpg", #"ham_and_cheese_panini.jpg", #"ham_and_egg_sandwich.jpg", #"hamburger.jpg", #"instant_noodle_with_egg.jpg", #"japanese_noodle_with_pork.jpg", #"mushroom_risotto.jpg", #"noodle_with_bbq_pork.jpg", #"starbucks_coffee.jpg", #"thai_shrimp_cake.jpg", #"vegetable_curry.jpg", #"white_chocolate_donut.jpg", nil];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return images.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell;
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSArray *tab = [collectionView visibleCells];
for (int i = 0; i < [tab count]; ++i)
{
NSLog(#"%d", i);
}
[collectionView setBackgroundColor:[UIColor blueColor]];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
return cell;
}
Where am I wrong ? Thanks a lot.