UILabel In UICollectionViewCell is setting after scrolling? - ios

In my code,I was trying to use UIImage & UIabel in UICollectionViewCell.What I was doing If there is no image in UICollectionViewCell i want to shift UILabel upwards.So I used two frames of label and checked for if(UIImageView.image==nil) it is showing effect after scrolling?
What to do?
Here is the code what i did-
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *myCell=(UICollectionViewCell*)[self.view viewWithTag:1];
myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"Cell"
forIndexPath:indexPath];
UILabel *label=(UILabel*)[myCell viewWithTag:12];
UIImageView *collectionViewImage=(UIImageView*)[myCell viewWithTag:11];
NSString *collectionViewImageName=[imagesCollectionViewArray objectAtIndex:indexPath.row];
collectionViewImage.image=[UIImage imageNamed:collectionViewImageName];
if (collectionViewImage.image==nil) {
[collectionViewImage setBackgroundColor:[UIColor clearColor]];
[label setFrame:CGRectMake(35, 73, 309, 54)];
label.text=[lableColectionviewArray objectAtIndex:indexPath.row];
return myCell;
}else {
[label setFrame:CGRectMake(20, 294, 352, 49)];
label.text=[lableColectionviewArray objectAtIndex:indexPath.row];
return myCell;
}
}

Try to remove cell contentView subviews every collectionView
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
method call.
use
for([UIView *view in cell.contentView.subViews]){
[view removeFromSuperView];
view=nil;
}
and replace this line of code
UICollectionViewCell myCell=(UICollectionViewCell)[self.view viewWithTag:1];
With
UICollectionViewCell *myCell=nil;

Related

Add an image on collection view cell having background colour in iOS

i am working on my application in which I have to add an image to collection view cell on selection. Collection view cell is circular and filled with color, so on selection of cell, image of tick should be on that color.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
NSArray *hexaColor= _advantageModelObj.productDetail.carColorDetails;
CarColorDetailObject *carColor= [hexaColor objectAtIndex:indexPath.row];
NSString *hexCode= carColor.carColor.hexCode;
cell.layer.masksToBounds=TRUE;
cell.backgroundColor=[self colorWithHexString:hexCode];
cell.layer.cornerRadius= (roundf(cell.frame.size.width/2));
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"colorselected.png"]];
return cell;
}
Make global variable
int clickedIndex;
2 in viewDidLoad assign to clickedIndex
clickedIndex=-1;
in collectionView didSelectItemAtIndexPath
clickedIndex=(int)indexPath.row;
[collectionView reloadData];
4 add some line in collectionView cellForItemAtIndexPath
if(clickedIndex==indexPath.row){
UIImageView* imageView=[[UIImageView alloc]init];
[imageView setFrame:CGRectMake(10, 0, cell.contentView.size.width, cell.contentView.size.height)];
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#“tickImage”,]];
imageView.alpha=0.5;
[cell.contentView addSubview: imageView];
}

collectionview just filling one cell

I'm trying to fill a collectionview with data from an array which I've checked it has the data, but at runtime it's filling just the cell at index 0.
The collectionview is a list of friends in pages of 9 items showing photo and name. The number of items at section is working properly, I mean, if the array has 3 objects, the collectionview displays three cells but just the first one with the photo and name of the object, concretly the last one in the array, not the first one. And the other cells show the prototype cell.
I guess I'm dealing wrong with the indexpath of the collection view, but I have another one in my storyboard and works properly. This other one has only one cell per page, could be something related to this?
I paste my collectionview methods:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.miListaAmigos count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"friendCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *nameLabel = (UILabel *)[self.view viewWithTag:102];
nameLabel.font = [UIFont fontWithName:#"Ubuntu" size:12.0];
nameLabel.text = [[self.miListaAmigos objectAtIndex:indexPath.row] valueForKey:#"usr_username"];
return cell;
}
you not directly reuse UILabel from self.view, UILabel *nameLabel = (UILabel *)[self.view viewWithTag:102];
try this below code for solve your problem:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"friendCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
//Create UILabel here
UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 20)];
nameLabel.tag=100;
[cell addSubview:nameLabel];
nameLabel.font = [UIFont fontWithName:#"Ubuntu" size:12.0];
}
// Access label and reuse
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.text = [[self.miListaAmigos objectAtIndex:indexPath.row] valueForKey:#"usr_username"];
return cell;
}
Check your collectionView alloc and required deleagte:
UICollectionViewDelegate、UICollectionViewDataSource、 UICollectionViewDelegateFlowLayout.
e.g.
UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
[collectionLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:collectionLayout];
[collectionView registerClass:[CNCollectionViewCell class] forCellWithReuseIdentifier:cellIndetify];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
Your label is in self.view not in the cell... You really should make a subclass of UICollectionViewCell
File -> New -> File -> Cocoa Touch Class
Class: MYFriendCollectionViewCell
Subclass of: UICollectionViewCell
Also Create XIB: Tick
Language: Objective C
Look at MYFriendCollectionViewCell.xib and make your label inside here... connect the labels to an IBOutlet... don't use tags...
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
https://www.youtube.com/watch?v=GusRijNLUGg <- connecting IBOutlets
Add the line below in your ViewController
[self.collectionView registerNib:[UINib nibWithNibName:#"MYFriendCollectionViewCell" bundle:nil] forCellWithReuseIdentifier: CellIdentifier];
Change your cellForItem to:
static NSString *CellIdentifier = #"friendCell";
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MYFriendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath: indexPath];
cell.nameLabel.font = nameLabel.font = [UIFont fontWithName:#"Ubuntu" size:12.0]; // This ideally would be in MYFriendCollectionViewCell.m
cell.nameLabel.text = [[self.miListaAmigos objectAtIndex:indexPath.row] valueForKey:#"usr_username"];
return cell;
}

UICollectionView not registring cell

I am using two UICollectionView in same controller but when I am initialising second UICollectionView it is Giving exception like
Terminating app due to uncaught exception NSInternalInconsistencyException, reason: could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
This is my code:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView==collView) {
return arrDatabase.count;
}
else
return arrSearchResult.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier=#"cell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (collectionView==collView) {
objprop=[arrDatabase objectAtIndex:indexPath.row];
UIImageView *imgView=(UIImageView*)[cell viewWithTag:100];
//imgRecipe.image=[UIImage imageNamed:[ objectAtIndex:indexPath.row]];
imgView.image=[UIImage imageNamed:#"Hamburger.jpg"];
UILabel *lblName=(UILabel*)[cell viewWithTag:101];
lblName.text=[NSString stringWithFormat:#"%#",objprop.categoryName];
//cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
}
if (collectionView==collView1) {
NSString *str=#"setIdentifier";
UIImageView *imgView2;
UILabel *lblCat;
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
if (cell==nil) {
cell
=[[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 97, 101)];
imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(2, 13, 92, 76)];
lblCat=[[UILabel alloc]initWithFrame:CGRectMake(3, 94, 92, 13)];
}
imgView2.image=[UIImage imageNamed:#"hamburger.jpg"];
[cell.contentView addSubview:imgView2];
lblCat.backgroundColor=[UIColor clearColor];
lblCat.text=[NSString stringWithFormat:#"%#",[arrSearchResult[indexPath.row] objectAtIndex:0]];
[cell.contentView addSubview:lblCat];
}
return cell;
}
Please register the UICollectionViewCell class in viewDidLoad
like:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cell"];
set your cell identifier as in code

Create a gallery using UICollectionView iOS

I wanna create a gallery using uicollectionview above tab bar controller. the problem that i have is i’m getting a SIGABRT crash when i’m connecting uicollectionview datasource. but when i didnt connecting it, the controller view did not show any image. did somebody know what to do?
this is the code I've made :
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(#"stiker : %d", stickers1.count);
return stickers1.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
NSLog(#"indexpath row : %d", indexPath.row);
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[stickers1 objectAtIndex:indexPath.row]];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
NSLog(#"numberOfSectionsInCollectionView coll : %d, stic : %d", collectionView.numberOfSections, stickers1Collection.numberOfSections);
return collectionView.numberOfSections;
}
I've connected the uicollectionview datasource in xib
Set number of items
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
set content
Just add UIImageView and UIlabel and set there tags in storyboard assign data in cellForItemAtIndexPath method
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *ImageView = (UIImageView *)[cell viewWithTag:100];
ImageView.image = [UIImage imageNamed:[[arrCategoryValue objectAtIndex:indexPath.row] objectForKey:#"image"]];
UILabel *lbl = (UILabel *)[cell viewWithTag:200];
lbl.text = [[arrCategoryValue objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.tag = indexPath.row;
return cell;
}
for more info you can go here

UICollectionView reload data Issue

I have a problem with data reloading using UICollectionView. I have this array on the viewDidLoad to fill the UICollectionView.
array = [[NSMutableArray alloc] init];
[array addObject:#"1"];
[array addObject:#"2"];
[array addObject:#"3"];
[array addObject:#"4"];
[array addObject:#"5"];
[array addObject:#"6"];
and the method:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
//cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
myCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 100, 20)];
[titleLabel setText:[array objectAtIndex:indexPath.row]];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:titleLabel];
return cell;
}
I tap the UIButton and data is reloaded:
[myCollectionView reloadData];
Here how the data looks like before and after the reload:
You add a new label each time you tap the reload button. You should add the label once and change the label text according.
Here a simple example.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"Cell"
forIndexPath:indexPath];
[cell setMyTextLabel:indexPath.row];
return cell;
}
where MyCell will contains a UILabel and a property to modify its text.
I really suggest to take a look at Fun with UICollectionView code by #Ben Scheirman.
Hope that helps.
P.S. Rename myCell to MyCell. A class should start with an uppercase letter.
You are adding your label when you tap reload button. In that case you are adding label again and again...
So there are three solutions:
Make your cell reusable
Remove your cell from superview in reload method.
You can check if label's text length is not equal to zero. In that case no need to add that label and change text.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(), ^{
// ********* Changed *******
for (UIView *v in [cell.contentView subviews])
[v removeFromSuperview];
// ********** Changed **********
if ([self.collectionviewFlow.indexPathsForVisibleItems containsObject:indexPath]) {
NSString *img_name=[NSString stringWithFormat:#"%#_thumb%d.png",self.VaritiesName,(int)indexPath.row+1];
imageVw=[[UIImageView alloc]initWithImage:[UIImage imageNamed: img_name]];
imageVw.frame=CGRectMake(10,10,100,100);
[cell.contentView addSubview:imageVw];
}
});
cell.backgroundColor=[UIColor clearColor];
return cell;
}
It is quit late, but here is my solution.
if you used custom collectionviewcell
try to use 'func prepareForReuse()'

Resources