I create one UICollectionView sticky header section with this : sticky header section but when my UICollectionViewCell is 0 number header section in hide like this picture :
in my picture show me one header section (index:2) and other header section is hidden. please guide me !!! I want other show like index 2.
this is my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
Flow *layout=[[Flow alloc] init];
//layout.sectionInset = UIEdgeInsetsMake(20,0,40,0);
//layout.itemSize = CGSizeMake(100,100);
_collect=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
_collect.backgroundColor = [UIColor clearColor];
[_collect setDataSource:self];
[_collect setDelegate:self];
[_collect registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView"];
[_collect registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[self.view addSubview:_collect];
//NSLog(#"%#",self.array);
}
- (NSMutableArray*)array {
if (!_array) {
_array = [NSMutableArray array];
for (NSInteger i=0; i<SectionNum; i++) {
//NSMutableArray *inner = [NSMutableArray arrayWithObject:#"0"];
[_array addObject:#"0"];
}
}
return _array;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return SectionNum;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 2) {
return 10;
} else
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"cellIdentifier";
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
HeaderView *header = [_collect dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
header.backgroundColor = [UIColor colorWithWhite:0.75 alpha:.9];
header.titleLabel.font = [UIFont systemFontOfSize:24];
header.titleLabel.textAlignment = NSTextAlignmentCenter;
header.titleLabel.text = [NSString stringWithFormat:#"%ld",indexPath.section];
reusableview = header;
}
if (kind == UICollectionElementKindSectionFooter) {
UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:#"FooterView" forIndexPath:indexPath];
reusableview = footerview;
}
return reusableview;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100,100);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 50);
return headerSize;
}
Related
I have a small problem ..
For convenience and to make you understand better how to share the collection View:
Collectionview 1 has the customized segment control function (it has a custom UICollectionView custom class)
Collectionview 2 contains the cells that function as pages (it has a UICollectionViewController class).
I have an UIViewController that contains the collectionview1 that I used to create a custom segment control (like a menu)
The cursor indicating which cell was selected was created with a uiview in the collectionView 1 class.
In my main view controller in addition to having a collectionview I also inserted a container view that contains a collectionview controller that will be used to show the selected pages through a horizontal scroll.
My problem now appears when I want to pass the ContentOffset value into the collectionView2 scrollViewDidScroll to the cursor in the collectionView 1.
I tried to initialize the collectionview 1 class in the collectionView 2 class to use the cursor directly but I do not get any results.
Can you help me understand how to solve this problem?
This is Class CollectionView 1 ( Menu )
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setupControl];
}
return self;
}
-(void)setupControl {
self.delegate = self;
self.dataSource = self;
self.pagingEnabled = YES;
self.showsHorizontalScrollIndicator = NO;
UINib *nib = [UINib nibWithNibName:#"UDSectionNavCell" bundle: nil];
[self registerNib:nib forCellWithReuseIdentifier:#"SCC"];
[self layout];
_horizontalCursorBar = [[UDSectionNavCursor alloc] init];
_horizontalCursorBar.frame = CGRectMake(0, self.frame.size.height - 6, self.frame.size.width / 4, 3);
_horizontalCursorBar.layer.cornerRadius = 3;
_horizontalCursorBar.backgroundColor = [UIColor colorWithHexString:#"#C1C1C1" setAlpha:1];
_horizontalCursorBar.alpha = 1;
[self addSubview:_horizontalCursorBar];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
-(void)layout {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.itemSize = CGSizeMake(self.frame.size.width / 4, self.frame.size.height);
layout.minimumInteritemSpacing = 10;
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionViewLayout = layout;
[self reloadData];
}
-(NSArray *)dataPage {
return [NSArray arrayWithObjects:#"ATENEO",#"STATISTICHE",#"CALENDARIO",#"NOTIFICHE", nil];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self dataPage].count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *cellID = #"SCC";
UDSectionNavCell *cell = (UDSectionNavCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.titlePage.text = [[self dataPage] objectAtIndex:indexPath.item];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
[self scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
if (indexPath == newIndexPath) {
[self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
[UIView animateWithDuration:.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGFloat x = (indexPath.item) * self.frame.size.width / 4;
_horizontalCursorBar.frame = CGRectMake(x, self.frame.size.height - 6, self.frame.size.width / 4, 3);
} completion:nil];
}
This is the code of CollectionView 2 (page cell)
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
collectionLayout.minimumLineSpacing = 0;
self.collectionView.collectionViewLayout = collectionLayout;
[self.collectionView reloadData];
self.collectionView.pagingEnabled = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSArray *array = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor], [UIColor clearColor], [UIColor grayColor], nil];
cell.backgroundColor = array[indexPath.item];
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"%f",scrollView.contentOffset.x / 4);
}
I would need to pass the ContentOffset value of the scrollViewDidScroll method in the collectionView 2 class and use it in the collectionView class 1
I load a table with images and scroll up and down. After a minute or so, I get a "Received memory warning" notification. After seeing this message a few times, the app crashes.
I load by scrolling position 5 images every time the user scrolls to the bottom of the table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"aCell";
PostTable_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGFloat cellHeight = (screenWidth / 16) * 10 + self.view.frame.size.height * 0.09;
cell = [[PostTable_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withParentWidth:self.view.frame.size.width withCellHeight:cellHeight withParent:self withPostArray:[postsArray objectAtIndex:indexPath.row] withCurrentIndexPostArray:(int)indexPath.row];
[cell setUserInteractionEnabled:YES];
cell.selectionStyle = UITableViewCellStyleDefault;
tableView.separatorColor = [UIColor blackColor];
return cell;
}
Edit :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"aCell";
PostTable_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGFloat cellHeight = (screenWidth / 16) * 10 + self.view.frame.size.height * 0.09;
if (cell == nil)
{
cell = [[PostTable_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withParentWidth:self.view.frame.size.width withCellHeight:cellHeight withParent:self withPostArray:[postsArray objectAtIndex:indexPath.row] withCurrentIndexPostArray:(int)indexPath.row];
[cell setUserInteractionEnabled:YES];
cell.selectionStyle = UITableViewCellStyleDefault;
tableView.separatorColor = [UIColor blackColor];
}
else
{
[cell reloadCellContetWithParent:self withPostArray:[postsArray objectAtIndex:indexPath.row] withCurrentIndexPostArray:(int)indexPath.row];
}
return cell;
}
Edit 3 :
#implementation PostTable_Cell
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)dealloc
{
#ifdef DEBUG
NSLog(#"%# destoyed",NSStringFromClass([self class]));
#endif
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withParentWidth:(CGFloat)parentWidth withCellHeight:(CGFloat)celltHeight withParent:(id)parent withPostArray:(id)postArray withCurrentIndexPostArray:(int)IndexNumber
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
mainDalegate = (Main*)parent;
self.backgroundColor = [UIColor clearColor];
Post *curPost = (Post*)postArray;
postId = [curPost getPostId];
postText = [curPost getPostText];
postImageUrl = [curPost getImageUrl];
commentsCount = [curPost getCommentsCount];
likesCount = [curPost getLikesCount];
likeByMe = [curPost getLikeByMe];
facebookId = [curPost getCreatorFacebookId];
publisherName =[curPost getCreatorName];
currentIndexPostArray = IndexNumber;
[GeneralMethods setNew_width:parentWidth ToView:self];
[GeneralMethods setNew_height:celltHeight ToView:self];
[self cellBuilderForPost];
}
return self;
}
mainDalegate = (Main*)parent;
It looks like this might be your issue. Your cells are not getting released because you are creating a retain cycle.
rules to avoid retain cycles.
When I try to tap all collectionViewCells from top to bottom, lower cells which is probably reused can't be tapped.
I guess the cells is reused with selection condition.
How do I fix it?
ViewController.m
- (void)viewDidLoad
{
flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(106, 106);
collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) collectionViewLayout:flowLayout];
collectionView.allowsMultipleSelection = YES;
[collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:kCellIdentifier];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#", indexPath);
}
CustomCell.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//
}
}
In -collectionView:didSelectItemAtIndexPath:, call [self.collectionView deselectItemAtIndexPath:indexPath animated:YES]; to deselect the cell so that in case it gets reused, the selection won't be passed on to be reused.
Another way of doing this is to have your model track whether it is selected or not so that you can set the selected state in -collectionView:cellForItemAtIndexPath:.
I would like to imitate the grouped table of the standard Contacts app (left). Each section has gray line dividers above and below. In my grouped table (right), there is no line above each section. And even stranger, the middle section has no lines at all.
#import "GroupViewController.h"
#import "UserCollectionViewCell.h"
#import "UIView+position.h"
#import "UIColor+style.h"
#import "UserPublicViewController.h"
#import "RecipientsGroupsModel.h"
static NSString* AVATAR_CELL_ID = #"groupUserCollectionUserView";
static const CGFloat AVATAR_ITEM_SPACING = 16.0;
static const CGFloat AVATAR_LINE_SPACING = 9.0;
static const CGFloat AVATAR_MARGIN = 15.5;
static const CGFloat AVATAR_CELL_WIDTH = 45.0;
static const CGFloat AVATAR_CELL_HEIGHT = 58.0;
typedef enum {
GroupViewControllerTableSectionTitle,
GroupViewControllerTableSectionUsers,
GroupViewControllerTableSectionDelete
} GroupViewControllerTableSection;
#interface GroupViewController ()
#property(nonatomic, strong) Group* group;
#property(nonatomic, strong) UICollectionView* avatarCollectionView;
#property(nonatomic, readonly) CGFloat usersHeight;
- (void)didLongTapAvatars:(UILongPressGestureRecognizer*)gesture;
#end
#implementation GroupViewController
#pragma mark UIViewController
- (NSString*)title
{
return #"Group";
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor backgroundColor];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView reloadData];
[self.avatarCollectionView reloadData];
}
#pragma mark GroupViewController ()
- (void)didLongTapAvatars:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan) {
[self setEditing:!self.isEditing animated:YES];
}
}
- (CGFloat)usersHeight
{
return ceilf(
self.group.users.count
/ floorf(
(
self.view.width
- 2 * AVATAR_MARGIN
+ AVATAR_ITEM_SPACING
)
/ (
AVATAR_ITEM_SPACING
+ AVATAR_CELL_WIDTH
)
)
)
* (AVATAR_CELL_HEIGHT + AVATAR_LINE_SPACING)
+ AVATAR_LINE_SPACING;
}
#pragma mark GroupViewController
- (instancetype)initWithGroup:(Group *)group
{
if (self = [super init]) {
self.group = group;
}
return self;
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case GroupViewControllerTableSectionTitle:
case GroupViewControllerTableSectionDelete:
return 44.0f;
break;
case GroupViewControllerTableSectionUsers:
return self.usersHeight;
break;
default:
return 0.0f;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 22.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
switch (section) {
case GroupViewControllerTableSectionDelete:
return 22.0f;
break;
default:
return 0.0f;
}
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// iOS 7 applies a transclucent view if you don't return your own view
return [[UIView alloc] init];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] init];
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.isEditing) {
return GroupViewControllerTableSectionDelete + 1;
} else {
return GroupViewControllerTableSectionUsers + 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* identifier = [NSNumber numberWithInteger:indexPath.section].stringValue;
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
switch (indexPath.section) {
case GroupViewControllerTableSectionTitle:
cell.textLabel.text = self.group.title;
break;
case GroupViewControllerTableSectionUsers: {
if (self.avatarCollectionView == nil) {
UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionInset = UIEdgeInsetsMake(
AVATAR_LINE_SPACING,
AVATAR_MARGIN,
AVATAR_LINE_SPACING,
AVATAR_MARGIN
);
layout.itemSize = CGSizeMake(AVATAR_CELL_WIDTH, AVATAR_CELL_HEIGHT);
layout.minimumInteritemSpacing = AVATAR_ITEM_SPACING;
layout.minimumLineSpacing = AVATAR_LINE_SPACING;
self.avatarCollectionView = [[UICollectionView alloc]
initWithFrame:CGRectMake(
0.0,
0.0,
self.view.width,
self.usersHeight
)
collectionViewLayout:layout
];
self.avatarCollectionView.backgroundColor = [UIColor clearColor];
self.avatarCollectionView.dataSource = self;
self.avatarCollectionView.delegate = self;
[self.avatarCollectionView registerClass:[UserCollectionViewCell class] forCellWithReuseIdentifier:AVATAR_CELL_ID];
[self.avatarCollectionView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(didLongTapAvatars:)]];
}
[cell.contentView addSubview:self.avatarCollectionView];
break;
}
case GroupViewControllerTableSectionDelete:
cell.textLabel.text = #"Delete Group";
cell.textLabel.textColor = [UIColor redColor];
break;
}
}
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
#pragma mark UICollectionViewDataSource
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UserCollectionViewCell* cell;
cell = [self.avatarCollectionView dequeueReusableCellWithReuseIdentifier:AVATAR_CELL_ID forIndexPath:indexPath];
cell.user = [self.group.users objectAtIndex:indexPath.row];
cell.isEditing = self.isEditing;
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.group.users.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
#pragma mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
User* user = [self.group.users objectAtIndex:indexPath.row];
if (self.isEditing) {
if (self.group.users.count <= 1) {
[RecipientsGroupsModel removeGroup:self.group];
[self.navigationController popViewControllerAnimated:YES];
} else {
[self.group.users removeObject:user];
[RecipientsGroupsModel updateGroup:self.group];
[self.avatarCollectionView deleteItemsAtIndexPaths:#[indexPath]];
}
} else {
[self.navigationController pushViewController:[[UserPublicViewController alloc] initWithUser:user] animated:YES];
}
}
#end
You can use the UITableViewDelegate methods:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
where you can specify any height you want for header and footer of any section. Return 0 in case you do not need a header or footer.
Then use the other two methods:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
Inside each of them, you can write the custom code to make them look as you want:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
view.backgroundColor = [UIColor grayColor];
// ...
// Other customizations, like adding the gray line, etc.
// ...
return view;
i have multiple image in uicollectionview like grid view. i want select multiple image at a time but not working this code.please any one give idea about this code.
i have already tried this code but not working.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];
if (self.selectedItemIndexPath)
{
// if we had a previously selected cell
if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame)
{
// if it's the same as the one we just tapped on, then we're unselecting it
self.selectedItemIndexPath = nil;
}
else
{
// if it's different, then add that old one to our list of cells to reload, and
// save the currently selected indexPath
[indexPaths addObject:self.selectedItemIndexPath];
self.selectedItemIndexPath = indexPath;
}
}
else
{
// else, we didn't have previously selected cell, so we only need to save this indexPath for future reference
self.selectedItemIndexPath = indexPath;
}
// and now only reload only the cells that need updating
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
// I Have Drag the UICollectionView Controller in storyboard
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
arrImage = [[NSMutableArray alloc]initWithObjects:#"1.jpeg",#"2.jpeg",#"3.jpeg",#"4.jpeg",#"5.jpeg",#"6.jpeg",#"7.jpeg",#"8.jpeg",#"9.jpeg",#"10.jpeg",#"flower.jpeg",#"flower1.jpeg", nil];
[self.collectionView setAllowsMultipleSelection:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrImage count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
[self.view addSubview:recipeImageView];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.15];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
}