multiple selection in uicollectionview not working in ios - uicollectionview

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]];
}

Related

UICollectionViewCell which is reused can't be selected

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:.

how to display section headers when uicollectionviewcell is 0 number

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;
}

How cell swipe on clicking a button

I want to swipe cell on click a button . I am succesful on swiping a cell. But i want to swipe on button which is in cell. my code is
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleCell";
SimpleCell *cell = (SimpleCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
SimpleCell *cekks=[[SimpleCell alloc]init];
cekks.scrollButton.alpha =0.0;
NSString *titleString;
UIButton *sender = [[UIButton alloc]init];
//[sender setBackgroundImage:[UIImage imageNamed:#"swipe.png"] forState:UIControlStateNormal];
sender.tag = indexPath.row;
titleString =#"Send A Gift";
UITableViewRowAction *sendAGift = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:titleString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// [self deleteMail:[NSArray arrayWithObject:indexPath]:YES];
}];
[sendAGift setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"swipe.png"]]];
return #[sendAGift];
}
I think your class SimpleCell should contain the UIButton, in order perform reusing correctly.
It will be simpler if you create a custom UITableViewCell that contains all the UI actions over the cell and operate them within the cell, not in the UITableView itself.
Let's see an example:
Here's the customCell.h file:
#interface customCell : UITableViewCell {
UIButton *buttonSwipe;
}
#end
Here's the customCell.h file:
#import "customCell.h"
#implementation customCell
- (instancetype) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void) commonInit {
buttonSwipe = [UIButton... // Initialize here your button
[buttonSwipe addTarget:self action:#selector(swipeCell:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) swipeCell {
// Embed here the code that makes the effect of swipe the cell.
}
This is a fast-untested workaround with some predefined code, but I think it's a working example if you want to make your cells swipe with your own code.
But if you want a faster way, I recommend you to visit Chris Wendel and his SWTableViewCell on GitHub
You can call editActionsForRowAtIndexPath: method as
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:SELECTED_ROW_INDEX inSection:0];
[self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
try the below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
//swipe button allocation
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
btn.tag = indexPath.row;
[cell.contentView addSubview:btn];
[btn addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
cell.textLabel.text = [NSString stringWithFormat:#"%lu",indexPath.row];
return cell;
}
-(void)buttonTouched:(id)sender{
UIButton *btn = (UIButton *)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
[self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"edit");
// code
return nil;
}

UILabel will does not show up in collectionView Cell

I have a collectionView that will display a cell from the dataArray. The dataArray is changed to self.postsArray or self.eventsArray from a segmentControl as well as the reuseIdentifier to determine the cell(a post cell and a event cell). The events work fine, but the Post Cell, which contains a label with a tag of 95, does not work. When I NSLog(#"label: %#", label); I get nil. So i have no idea what the problem is.
#pragma mark Collection View:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [dataArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (dataArray == self.postsArray) {
UILabel *label = (UILabel *)[cell viewWithTag:95];
[label.text isEqualToString:#"Hello"];
NSLog(#"label: %#", label);
return cell;
}
if (dataArray == self.eventsArray) {
PFObject *temp = [dataArray objectAtIndex:indexPath.row];
UIButton *eventButton = (UIButton *) [cell viewWithTag:10];
NSLog(#"Event: %#", eventButton);
UIButton *groupButton = (UIButton *) [cell viewWithTag:11];
[eventButton setTitle:[temp objectForKey:#"Title"] forState:UIControlStateNormal];
[groupButton setTitle:[temp objectForKey:#"Group_Name"] forState:UIControlStateNormal];
eventButton.tag = indexPath.row;
groupButton.tag = indexPath.row;
return cell;
}
return cell;
}
#pragma mark SegmentControl:
- (void) segmentChanged: (id) sender{
UISegmentedControl *seg = (UISegmentedControl *) sender;
if (seg.selectedSegmentIndex == 0) {
dataArray = self.postsArray;
reuseIdentifier = #"postsCell";
[self.collectionView reloadData];
}
if (seg.selectedSegmentIndex == 1) {
dataArray = self.eventsArray;
reuseIdentifier = #"eventCell";
[self.collectionView reloadData];
}
}
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:groupReuseIdentifier]; was left in my viewDidLoad and was hiding my first cells

How to link a UICollectionViewCell to a UICollectionView

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.