I am using UICollectionView and I want to add UISearchBar or UISearchDisplayController to it.
Can i do that. I want some example please.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
CollectionReusable *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeadCell" forIndexPath:indexPath];
reusableview = headerView;
}
if (kind == UICollectionElementKindSectionFooter) {
CollectionReusable *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:#"FootCell" forIndexPath:indexPath];
reusableview = footerView;
}
return reusableview;
}
It's a bit late but this repository contains a great piece of code dealing with this issue.
Related
I have a collectionview with custom collectionviewcell. I want to make a gallery that only one image display on the screen at any time. What i have to do ?
Here is my code :
[myCollectionView registerClass:[myCustomCollectionViewCell class] forCellWithReuseIdentifier:"myIdentifier"];
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCustomCollectionViewCell *cell = (myCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"myIdentifier" forIndexPath:indexPath];
cell.layer.masksToBounds=NO;
cell.imageView.image = [UIImage imageNamed:#"image"];
return cell;
}
First, register your collection view class like below
[yourCollectionView registerClass:[yourCustomCollectionViewCell class] forCellWithReuseIdentifier:"yourIdentifier"];
Second, implement collection view datasource method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
yourCustomCollectionViewCell *cell = (yourCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"yourIdentifier" forIndexPath:indexPath];
cell.layer.masksToBounds=NO;
cell.imageView.image = [UIImage imageNamed:#"yourImageName"];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// update size accordingly
return CGSizeMake(100,100);
}
I have a tableview which have 4 row. then i want to insert a collectionview into tableview cell and each collectionview have different cell. But i can't do that. can any one help me out or any suggesition?
You can give different CellIdentifiers for every TableViewCell and createa different rows in InterfaceBuilder or inside
cellForRowAtIndexPath
Dont forget to provide dataSource and delegate for every collectionView that you will create for TableViewCell
Pseudocode:
YN Means YourNamespace
YNDataSourceDelegate1* data1;
YNDataSourceDelegate2* data2;
YNDataSourceDelegate3* data3;
YNDataSourceDelegate4* data4;
const NSInteger COLLECTION_VIEW_TAG = 1001;
NSArray* providers=#[data1,data2,data3,data4];
NSArray* rowIds=#[#"row1",#"row2",#"row3",#"row4"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rowIds[indexPath.row]];
UICollectionView * collectionView = (UICollectionView*)[cell viewWithTag:COLLECTION_VIEW_TAG];
collectionView.dataSource = providers[indexPath.row];
return cell;
}
This code will help you
yourViewController.h
#interface testingvc : UIViewController
<
UITableViewDelegate,
UITableViewDataSource,
UICollectionViewDelegate,
UICollectionViewDataSource
>
{
IBOutlet UITableView *tbleeView;
}
yourViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
tbleeView.delegate=self;
tbleeView.dataSource=self;
tbleeView.pagingEnabled=YES;
}
yourTableViewCell.h(UITableViewCell)
#property(strong,nonatomic)IBOutlet UICollectionView *clview;
yourViewController.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete implementation, return the number of rows
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *identifierDriver = #"abcdefgh";
yourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierDriver forIndexPath:indexPath];
//testing table cell is a UITableViewCell
if (cell == nil)
{
cell = [[testingTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierDriver];
}
cell.backgroundColor=[UIColor yellowColor];
//#property(strong,nonatomic)IBOutlet UICollectionView *clview;-this method is implement in UITableViewCell
// call collection view delegate method in table view cell.
cell.clview.delegate=self;
cell.clview.dataSource=self;
cell.clview.parentIndexpath = indexPath;
cell.clview.backgroundColor=[UIColor magentaColor];
cell.clview.pagingEnabled=YES;//optional
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSArray *collectionViewArray = [_arrayProducts [[(IndexedCollectionView *)collectionView parentIndexpath].row]objectForKey:#"inventory"];//arrProducts is an array where i get all the data from webservice
return [collectionViewArray count];
}
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
yourColloectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"abcdef" forIndexPath:indexPath];
NSArray *Products = [_arrayProducts[[(yourCollectionView *)collectionView parentIndexpath].row]objectForKey:#"inventory"];//arrProducts is an array where i get all the data from webservice
NSLog(#"object ibde xis ----%ld",[Products count]);
if (indexPath.row>[Products count]-1 )
{
NSLog(#"index not found");
}
else
{
NSDictionary *dictdata = [Products objectAtIndex:indexPath.row];
NSLog(#"dictdata is =====%#",dictdata);
//do your code here
}
if (indexPath.row % 2)
{
cell.backgroundColor=[UIColor brownColor];;
}
else
cell.backgroundColor=[UIColor redColor];;
return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (void)collectionView:(IndexedCollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger tableindex = collectionView.parentIndexpath.row;
NSLog(#"collection path row is ----%ld",tableindex);
NSLog(#"collection path row is ----%ld",indexPath.row);
//you can get all that index path number . Enjoy :)
}
I got a UICollectionView that I have created programmatically. I would like to have a collection view to behave in the following direction:
User touches cell
hidden uiview that cover image
User touches cell again
show uiview to cover the image
1.) Before user touch image in cell it look like this
http://upload.siamza.com/1811355
2.) After user touch image in cell it will look like this
http://upload.siamza.com/1811359
3.) If user touch image again it 'll look like 1.)
right now my select and deselect is look like this:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView == genrescollectView){
genresRecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"genresRecCell" forIndexPath:indexPath];
cell.checkforselectView.hidden = FALSE;
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView == genrescollectView){
genresRecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"genresRecCell" forIndexPath:indexPath];
cell.checkforselectView.hidden = TRUE;
}
}
but it seen to be not working at
cell.checkforselectView.hidden = FALSE;
and
cell.checkforselectView.hidden = TRUE;
I have check in cellForItemAtIndexPath for work or not by adding
cell.checkforselectView.hidden = FALSE;
and it work so I wonder anyone could help me with this problem and this is now in cellForItemAtIndexPath
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
genresRecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"genresRecCell" forIndexPath:indexPath];
NSString *imageGenres = [genresData objectAtIndex:indexPath.row];
cell.genresImage.image = [UIImage imageNamed:imageGenres];
NSString *textToParse = imageGenres;
NSArray *components = [textToParse componentsSeparatedByString:#"."];
NSString *genresText = [components firstObject];
cell.labelGenres.text = genresText;
cell.genresImage.layer.cornerRadius = 10.0f;
cell.genresImage.layer.masksToBounds = YES;
//---------> this is where I check whether it work or not(and it work).
cell.checkforselectView.hidden = FALSE;
return cell;
}
Thank in advance :)
in
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
use
[collectionView cellForItemAtIndexPath:indexPath]
instead of dequeing a new cell
I have a UIViewController with a UICollectionView on it but Xcode doesn't seem to look like every tutorial I find on the Internet or on YouTube - When I drag a UICollectionViewCell to place in the UICollectionView, it won't let me place it.
Now I'm confused as to how I can link my cell to the UICollectionView.
This is the viewController.h file:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.imagesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"imageCell" forIndexPath:indexPath];
NSString *myImageString = [self.imagesArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:myImageString];
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100.0, 100.0);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
//[self.collectionView registerClass:[ImageViewCell class] forCellWithReuseIdentifier:#"imageCell"];
self.imagesArray = #[#"shirt1.PNG", #"pants.png", #"pants2.png"];
}
I'm not using a Storyboard interface but individual xib's. When I run this all that appears is the blank black screen. What am I missing?
-registerNib:forCellWithReuseIdentifier:
If you have your cell defined in a NIB, then you register that NIB with the collection view. That is how the collection view know what to load when -dequeueReusableCellWithReuseIdentifier:forIndexPath: is called.
- (void)viewDidLoad
{
// …
UINib nib = [UINib nibWithNibName:#"<the name of your xib>" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:#"imageCell"];
// …
}
Because you didn't have new a object of UICollectionViewCell in this method:
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
The "cell" in your method must be set to nil.
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]];
}