Reusability issue on UICollectionView - ios

I had worked with UITableView but I have never ever use of UICollectionView in my apps. So I want to create UICollectionView programmatically.
Following is my code:
UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 43, self.view.frame.size.width, self.view.frame.size.height - 84) collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
layout.minimumInteritemSpacing = 5;
[_collectionView setBackgroundColor:self.view.backgroundColor];
[self.view addSubview:_collectionView];
Delegate and Datasource methods.
#pragma mark -
#pragma mark - UITableView Delegate Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
if (cell.selected)
cell.backgroundColor = [UIColor lightGrayColor]; // highlight selection cell
else
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background-grid.png"]]; // Default Cell
UIImageView *imgPhoto = [[UIImageView alloc] init];
imgPhoto.userInteractionEnabled = YES;
imgPhoto.backgroundColor = [UIColor grayColor];
imgPhoto.frame = CGRectMake(3.5, 5, 90, 80);
imgPhoto.clipsToBounds = YES;
imgPhoto.image = [UIImage imageNamed:#"product.png"];
[cell.contentView addSubview:imgPhoto];
UILabel *lblCategoryTitle = [[UILabel alloc] init];
[lblCategoryTitle setFont: [UIFont fontWithName:#"OpenSans-Bold" size:14]];
lblCategoryTitle.textAlignment = NSTextAlignmentCenter;
lblCategoryTitle.frame = CGRectMake(3.5, 90, 90, 24);
lblCategoryTitle.textColor = [UIColor blackColor];
lblCategoryTitle.text = #"Product 1";
lblCategoryTitle.backgroundColor = [UIColor clearColor];
lblCategoryTitle.numberOfLines = 2;
[cell.contentView addSubview:lblCategoryTitle];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(97, 118);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor lightGrayColor]; // highlight selection
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background-grid.png"]]; // default cell
}
Then my screen Look like
Question 1 - Look at above screen, you will see that 1st and 3rd item is looking blur (see Product 1 ) then 2nd/middle item ? why this is happening ?
And whenever I scroll up/down UICollectionView then items are overwrite, Look at Next image
After looked this image, from my experience of UITableView, it's happening because of Reusability of cell of UICollectionView.
Question 2 - Then how can i solve it?
Please give my your suggestion and help me on this issue.
EDITED: (suggestion of #Dima)
Custom cell
.h file
#import <UIKit/UIKit.h>
#interface customeGridCell : UICollectionViewCell
#property (nonatomic, strong) UIImageView *imgPhoto;
#property (nonatomic, strong) UILabel *lblCategoryTitle;
#end
.m file
#import "customeGridCell.h"
#implementation customeGridCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.imgPhoto = [[UIImageView alloc] init];
self.imgPhoto.userInteractionEnabled = YES;
self.imgPhoto.backgroundColor = [UIColor grayColor];
self.imgPhoto.frame = CGRectMake(3.5, 5, 90, 80);
[self addSubview:self.imgPhoto];
self.lblCategoryTitle = [[UILabel alloc] init];
[self.lblCategoryTitle setFont: [UIFont fontWithName:#"OpenSans-Bold" size:14]];
self.lblCategoryTitle.textAlignment = NSTextAlignmentCenter;
self.lblCategoryTitle.frame = CGRectMake(3.5, 90, 90, 24);
self.lblCategoryTitle.textColor = [UIColor blackColor];
self.lblCategoryTitle.backgroundColor = [UIColor clearColor];
self.lblCategoryTitle.numberOfLines = 2;
[self addSubview:self.lblCategoryTitle];
}
return self;
}
And code of cellForItemAtIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
customeGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
if (cell.selected)
cell.backgroundColor = [UIColor lightGrayColor]; // highlight selection cell
else
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background-grid.png"]]; // Default Cell
cell.imgPhoto.image = [UIImage imageNamed:#"product.png"];
cell.lblCategoryTitle.text = #"Product 1";
return cell;
}

The problem is in your collectionView:cellForItemAtIndexPath: method. You are adding those subviews every single time a cell is reused, on top of each other.
You should create a UICollectionViewCell subclass and add all of the extra subviews you want into its initializer. This will make sure they only get added once.
sample code:
Here is an example of how you would subclass UICollectionViewCell
#interface MyCustomCell : UICollectionViewCell
#property (nonatomic, strong) UILabel *customLabel;
#property (nonatomic, strong) UIImageView *customImageView;
#end
// in implementation file
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// initialize label and imageview here, then add them as subviews to the content view
}
return self;
}
Then when you are grabbing a cell you just do something like:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
if (cell.selected)
cell.backgroundColor = [UIColor lightGrayColor]; // highlight selection cell
else
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background-grid.png"]]; // Default Cell
cell.customImageView.image = // whatever
cell.customLabel.text = // whatever
return cell;
}

You can do it with two way.
Remove UILabel form view.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
for (UILabel *lbl in cell.contentView.subviews)
{
if ([lbl isKindOfClass:[UILabel class]])
{
[lbl removeFromSuperview];
}
}
UILabel *lblCategoryTitle =[[UILabel alloc]init];
[lblCategoryTitle setFont: [UIFont fontWithName:#"OpenSans-Bold" size:14]];
lblCategoryTitle.textAlignment = NSTextAlignmentCenter;
lblCategoryTitle.frame = CGRectMake(3.5, 90, 90, 24);
lblCategoryTitle.textColor = [UIColor blackColor];
lblCategoryTitle.text = #"Product 1";
lblCategoryTitle.backgroundColor = [UIColor clearColor];
lblCategoryTitle.numberOfLines = 2;
[cell.contentView addSubview:lblCategoryTitle];
return cell;
}
Use tag to get Label
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
UILabel *lblCategoryTitle =(UILabel *) [cell viewWithTag:5];
if (!lblCategoryTitle) {
lblCategoryTitle=[[UILabel alloc]init];
[cell.contentView addSubview:lblCategoryTitle];
}
[lblCategoryTitle setFont: [UIFont fontWithName:#"OpenSans-Bold" size:14]];
lblCategoryTitle.tag=5;
lblCategoryTitle.textAlignment = NSTextAlignmentCenter;
lblCategoryTitle.frame = CGRectMake(3.5, 90, 90, 24);
lblCategoryTitle.textColor = [UIColor blackColor];
lblCategoryTitle.text = #"Product 1";
lblCategoryTitle.backgroundColor = [UIColor clearColor];
lblCategoryTitle.numberOfLines = 2;
return cell;
}

Related

How can we change table-list title color when we tapped on it in ios

Hi i am beginner in Ios and in my project i have added some labels and image on table-list cell ok they have added so for everything is ok
Here my main requirement is when we tapped on table-list cell labels colors must be change and image need to be change as like below image
for this i have written some code but that's not working pleas help me
my code:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
[cell.contentView addSubview:strip];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
[cell.contentView addSubview:TitleLbl];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
[cell.contentView addSubview:img];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
TitleLbl.textColor = [UIColor darkGrayColor];
strip.backgroundColor = [UIColor whiteColor];
img.image = [UIImage imageNamed:#"rahul.png"];
}
The easy way would be to create a custom UITableViewCell and then in the delegate functions of UITableView get the instance of CustomTableViewCell and set the Labels textColor.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
CustomTableViewCell *cell =(CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.TitleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColr];
cell.img.image = [UIImage imageNamed:#"rahul.png"];
}
PS: there are many other work arounds for this...it depends on how do you want to implement the code.
Other way,
//set flag variable as
#property (strong ,nonatomic) NSIndexPath *selectedIndexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedIndexPath = indexPath;
[tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if(indexPath == selectedIndexPath)
{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
[cell.contentView addSubview:strip];
[cell.contentView addSubview:TitleLbl];
[cell.contentView addSubview:img];
}
//This code is not tested.
I suppose you are referring to the tableview header.
You can only set tableview header view to your own custom view using the tableHeaderView property of the tableview. You can set the background color of your custom header view normally.
E.g.
self.tableView.tableHeaderView = myHeaderView;
Alternatively, if you want to modify the section header view, you can use one of the following delegate methods:
tableView:viewForHeaderInSection:
dequeueReusableHeaderFooterViewWithIdentifier:
and return your custom header view there.
From the code, I can see you hold the reference for properties of last known strip, TitleLbl and img, which will always be the components from last UITableViewCell accessed from
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Since your cell has an orange strip on left side, I would prefer creating a custom UITableViewCell to hold the references.
#interface CustomCell : UITableViewCell
#property (nonatomic,strong) UIView *strip;
#property (nonatomic,strong) UILabel *titleLbl;
#property (nonatomic,strong) UIImageView *imgView;
#end
then with a custom cell, your code can be
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = #"cellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// create and add components
cell.strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
[cell.contentView addSubview:cell.strip];
cell.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
[cell.contentView addSubview:cell.titleLbl];
cell.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
[cell.contentView addSubview:cell.imgView];
}
// modify existing components
cell.strip.backgroundColor = [UIColor orangeColor];
cell.titleLbl.text = [Right_Menu_array objectAtIndex:indexPath.row];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.titleLbl.font = [UIFont systemFontOfSize:15];
cell.imgView.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if ([cell isSelected]) {
// remember to keep the changes for cell in it's selected state
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.strip.backgroundColor = [UIColor whiteColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
also you can override the method in your custom cell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}

Application crash when set header of UICollectionView horizontally

I am pretty new in UICollectionView. And I am really tired to find out the solution. I am trying to add Header in 3 Horizontally Row. I am using Collection view flow layout.
Here is my code which I implement:
- (void)awakeFromNib {
self.collectionView.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.itemSize = CGSizeMake(130.0, 170.0);
[self.collectionView setCollectionViewLayout:flowLayout];
// Register the colleciton cell
[_collectionView registerNib:[UINib nibWithNibName:#"ORGArticleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"ORGArticleCollectionViewCell"];
[self.collectionView registerClass:[_HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView"];
}
#pragma mark - UICollectionViewDataSource methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0) {
return [self.collectionData count];
}
else if(section == 1)
{
return [self.collectionData1 count];
}
else
{
return [self.collectionData2 count];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ORGArticleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"ORGArticleCollectionViewCell" forIndexPath:indexPath];
cell.articleTitle.text = [self.collectionData objectAtIndex:[indexPath row]];
NSString *URL = [self.collectionImageData objectAtIndex:indexPath.row];
[cell.articleImage setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:#"profile-image-placeholder"]];
cell.articleImage.contentMode = UIViewContentModeScaleToFill;
cell.articlePrice.text = [self.collectionDataPric objectAtIndex:[indexPath row]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
[[NSNotificationCenter defaultCenter] postNotificationName:#"didSelectItemFromCollectionView" object:cellData];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
UILabel *label = (UILabel *)[headerView viewWithTag:10];
if (!label) {
label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
label.tag = 10;
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor darkGrayColor];
[headerView addSubview:label];
}
label.text = [NSString stringWithFormat:#"Section %d", indexPath.section];
return headerView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 44);
return headerSize;
}
My App crash in viewForSupplementaryElementOfKind method when I initialize the header view.
Following are the crash log:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionHeader at path {length = 2, path = 0 - 0}'
The problem with your code is [headerView addSubview:label]; this is called only when label is equal to nil. but you have to call this statement every time when viewForSupplementaryElementOfKind is called.
Update your viewForSupplementaryElementOfKind function with following code.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
UILabel *label = (UILabel *)[headerView viewWithTag:10];
if (!label) {
label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
label.tag = 10;
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor darkGrayColor];
}
label.text = [NSString stringWithFormat:#"Section %d", indexPath.section];
[headerView addSubview:label];
return headerView;
}

UICollectionView : two columns

I have a collectionview that will be a menu with two columns, without spacing: so each cell with width 160. And I need a line separating each cell.
Each cell contains an imageview and a label.
I'm trying to add a line separator between the cells like this: How to add views between UICollectionViewCells in a UICollectionView?
But the result is:
Code:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 2;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return [options count];
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"MenuOptionCell";
UICollectionViewCell *cell = [self.menu dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *img = (UIImageView*) [cell viewWithTag:100];
UILabel *lbl = (UILabel*) [cell viewWithTag:90];
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+cell.frame.size.height, cell.frame.size.width-10, 1)];
seperatorView.backgroundColor = [UIColor whiteColor];
[self.menu addSubview:seperatorView];
[self.menu bringSubviewToFront:seperatorView];
UIView *seperatorView2 = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+cell.frame.size.width-10, cell.frame.origin.y, 1, cell.frame.size.height)];
seperatorView.backgroundColor = [UIColor whiteColor];
[self.menu addSubview:seperatorView2];
[self.menu bringSubviewToFront:seperatorView2];
lbl.text = #"TEST";
img.image = [UIImage imageNamed:#"test.png"];
return cell;
}

View description in myUICollectionView didn't work

I'm a beginner to learn ios. It's nice to be here! I have some problem using the UICollectionView.
I've add this in myUICollectionCell.m and linked the item to storyBoard
#import "QuartzCore/QuartzCore.h"
#implementation myCollectionViewCell
#synthesize myImageView
-(instancetype)initWithFrame:(CGRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 60, 60)];
myImageView.layer.cornerRadius = view2.frame.size.width/2;
myImageView.layer.masksToBounds = YES;
[self.contentView addSubview:myImageView];
}
return self;
}
#end
ViewController.m
#import "myCollectionViewCell.h"
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = #"cell";
myCollectionViewCell *cell = (myCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.myImageview.image = [UIImage imageNamed:#"2.jpg"];
return cell;
}
myImageView still didn't change to circle as I described.
It can only work when I put the description into collectionView:cellForItemAtIndexPath:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = #"cell";
myCollectionViewCell *cell = (myCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.myImageview.image = [UIImage imageNamed:#"2.jpg"];
myImageView.layer.cornerRadius = view2.frame.size.width/2;
myImageView.layer.masksToBounds = YES;
return cell;
}
why is that? I'm confusing with the place where I should describe my view on my cell.
It seems you are using xib or storyboard, so it won't call initWithFrame: of UICollectionViewCell.
Using awakeFromNib instead:
-(void)awakeFromNib
{
[super awakeFromNib];
//Is myImageView an outlet of storyboard? if it is you needn't realloc a UIImageView instance
//myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 60, 60)];
myImageView.layer.cornerRadius = 5.0;
myImageView.layer.masksToBounds = YES;
//[self.contentView addSubview:myImageView];
}

UICollectionView inside UITableViewCell - programmatically

Basically, I am trying to obtain something similar to the result of this tutorial: http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
I am quite lost in getting it working as I would like... my goal is:
Changing the collectionViewCell's from the tutorial to custom cells, containing for example a UILabel...
what code would I need to change? I have tried changing code in the following lines (from AFTableViewCell.h) but haven't gotten it working...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing= 100000.0f;
layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 0);
layout.itemSize = CGSizeMake(40, 22);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor blackColor];
self.collectionView.showsHorizontalScrollIndicator = YES;
[self.contentView addSubview:self.collectionView];
return self;
}
I would like to add, for example, the corresponding tableviewcell + collectionviewcell numbers on each label;
my idea was (excuse my poor knowledge of iOS... I am beginning):
changing the registerClass method for a custom cell:
[self.collectionView registerClass:[MyCustomCollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
having the MyCustomCollectionViewCell code the following in its .h file:
#interface MyCustomCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) UILabel *simpleLabel;
#end
and then, I thought I would only have to add the following to where, in the tutorial, the background for each UICollectionViewCell background was set (AFViewController.m):
-(MyCustomCollectionViewCell *)collectionView:(AFIndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
NSArray *collectionViewArray = self.colorArray[collectionView.index];
cell.backgroundColor = collectionViewArray[indexPath.item];
cell.simpleLabel.text = #"hi";
return cell;
}
However, no label is displayed... (just tried to make it display "hi" for ease of testing)
Fixed with help from this question: Programmatically Creating UILabel
Custom CollectionViewClass not needed, just edited the following method in AFViewController.m:
-(UICollectionViewCell *)collectionView:(AFIndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
NSArray *collectionViewArray = self.colorArray[collectionView.index];
cell.backgroundColor = collectionViewArray[indexPath.item];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,40,22)];
fromLabel.text = #"hi";
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[cell addSubview:fromLabel];
return cell;
}

Resources