The working is simple. I have a collectionview in iOS. I am receiving number of items as to be selected. I want to show this as ON . Rest should be switched OFF. User interaction is disabled.
you can set it in below method but you have to set proper Code.
-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//stuff
if(indexPath.row %2 ==0) //replace this with your condtion.
{
cell.selected = YES;
cell.userInteractionEnabled = YES;
}
else
{
cell.selected = NO;
cell.userInteractionEnabled = NO;
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
loyalityCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
NSString *userStamps = [userDefault objectForKey:#"stamps"];
for (int i=0; i<=userStamps.intValue; i++)
{
if (indexPath.row < userStamps.integerValue)
{
cell.stampImage.image = [UIImage imageNamed:#"stamphover.png"];
cell.selected= true;
cell.userInteractionEnabled= NO;
}
else
{
cell.stampImage.image= [UIImage imageNamed:#"stamp.png"];
cell.selected = true;
cell.userInteractionEnabled= NO;
}
}
return cell;
}
Related
I have three custom UICollectionViewCell.
RepoOfTheMonthCell
FromTheEditorsCell
WorldPremiereCell
and I have a NSArray contains order of these cells and contents which these cells should show. ('cellClass' is a class.)
(
"<RSHomeItem> \n [title]: REPO\n OF THE\n MONTH\n [img]: https://nexusrepo.kro.kr/repostore/img0.png\n [url]: https://nexusrepo.kro.kr/\n [cellClass]: RepoOfTheMonth\n [subtitle]: <nil>\n</RSHomeItem>",
"<RSHomeItem> \n [title]: REPO STORE\n [img]: https://nexusrepo.kro.kr/repostore/img1.png\n [url]: <nil>\n [cellClass]: FromTheEditors\n [subtitle]: Welcome to the\n Repo Store!\n</RSHomeItem>",
"<RSHomeItem> \n [title]: FEATURED REPO\n [img]: https://nexusrepo.kro.kr/repostore/img2.png\n [url]: https://nexusrepo.kro.kr\n [cellClass]: WorldPremiere\n [subtitle]: <nil>\n</RSHomeItem>"
)
So, In - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
I looped the NSArray like this.
self.listHome is a NSArray. and this array is special so I need to use RSHomeItem(JSONModel) to get value of 'cellClass' in string.
for (RSHomeItem *item in self.listHome) {
NSLog(#"class: %#", item.cellClass);
}
this method logged
class: RepoOfTheMonth
class: FromTheEditors
class: WorldPremiere
So I wrote this
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
int i = 0;
for (RSHomeItem *item in self.listHome) {
NSIndexPath *iIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
if ([item.cellClass isEqualToString:#"RepoOfTheMonth"]) {
RepoOfTheMonthCell *repo = [collectionView dequeueReusableCellWithReuseIdentifier:#"RepoOfTheMonth" forIndexPath:iIndexPath];
[repo.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
[repo withTitle:item.title URL:item.url];
return repo;
} else if ([item.cellClass isEqualToString:#"FromTheEditors"]) {
FromTheEditorsCell *editors = [collectionView dequeueReusableCellWithReuseIdentifier:#"FromTheEditors" forIndexPath:iIndexPath];
[editors.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
editors.titleLabel.text = item.title;
editors.subtitleLabel.text = item.subtitle;
return editors;
} else if ([item.cellClass isEqualToString:#"WorldPremiere"]) {
WorldPremiereCell *premiere = [collectionView dequeueReusableCellWithReuseIdentifier:#"WorldPremiere" forIndexPath:indexPath];
[premiere.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
[premiere withTitle:item.title URL:item.url];
return premiere;
}
i = i + 1;
}
return nil;
}
It builds well but It shows three RepoOfTheMonthCell with the same data instead of showing cells in this order.
RepoOfTheMonthCell
FromTheEditorsCell
WorldPremiereCell
numberOfSection is “1” and numberOfRow is “self.listHome.count”
Ok, I didn't need to loop it.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RSHomeItem *item = self.listHome[indexPath.row];
if ([item.cellClass isEqualToString:#"RepoOfTheMonth"]) {
RepoOfTheMonthCell *repo = [collectionView dequeueReusableCellWithReuseIdentifier:#"RepoOfTheMonth" forIndexPath:indexPath];
[repo.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
[repo withTitle:item.title URL:item.url];
return repo;
}
if ([item.cellClass isEqualToString:#"FromTheEditors"]) {
FromTheEditorsCell *editors = [collectionView dequeueReusableCellWithReuseIdentifier:#"FromTheEditors" forIndexPath:indexPath];
[editors.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
editors.titleLabel.text = item.title;
editors.subtitleLabel.text = item.subtitle;
return editors;
}
if ([item.cellClass isEqualToString:#"WorldPremiere"]) {
WorldPremiereCell *premiere = [collectionView dequeueReusableCellWithReuseIdentifier:#"WorldPremiere" forIndexPath:indexPath];
[premiere.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
[premiere withTitle:item.title URL:item.url];
return premiere;
}
return nil;
}
How to Limit UICollectionViewCell to 10 or 20? If UICollectionViewCell load so many cell from API.
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.items.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
eventisi* list = [_items objectAtIndex:indexPath.row];
UIImageView *iconEvent = (UIImageView*)[cell viewWithTag:1];
NSURL *url = [NSURL URLWithString:list.EVENT_IMAGE];
[iconEvent setImageWithURL:url usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
iconEvent.clipsToBounds = YES;
// Configure the cell
UILabel *judul = (UILabel *)[cell viewWithTag:2];
judul.text = list.EVENT_NAME;
judul.numberOfLines =2;
judul.lineBreakMode = NSLineBreakByWordWrapping;
judul.font = [UIFont systemFontOfSize:10];
return cell;
}
Limit the elements in self.items. The amount of cells displayed by a UICollectionView depends on the collectionView:numberOfItemsInSection: method, which entirely depends on the number of elements in self.items.
If you want to display a maximum of 20 UICollectionViewCells (depending on the number of items in self.items ) :
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if(self.items.count >= 20) {
return 20;
}
return self.items.count;
}
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;
}
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]];
}
I have a UISearchDisplayController properly connected to the header of a custom UITableView in IB. However, when I search for anything the searchResultsTableView only displays "No Results", and I cannot seem to find where the code is incorrect.
searchResults Property
- (NSMutableArray *)searchResults {
if (!_searchResults) {
_searchResults = [[NSMutableArray alloc] initWithCapacity:self.listingPreviews.count];
}
return _searchResults;
}
Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger numberOfRows = 0;
if (tableView == self.tableView) {
numberOfRows = self.listingPreviews.count;
} else {
numberOfRows = self.searchResults.count;
}
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Listing";
ListingPreviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[ListingPreviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
if (tableView == self.tableView) {
cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row];
} else {
NSLog([[self.searchResults objectAtIndex:indexPath.row] title]);
cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Search Bar Delegate Method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSInteger searchTextLength = searchText.length;
for (ListingPreview *listingPreview in self.listingPreviews) {
if ((listingPreview.title.length >= searchTextLength) && ([listingPreview.title rangeOfString:searchText].location != NSNotFound)) {
[self.searchResults addObject:listingPreview];
}
}
}
Just try this instead:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Listing";
ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
if (tableView == self.tableView) {
cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row];
} else {
NSLog([[self.searchResults objectAtIndex:indexPath.row] title]);
cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Please note that the change was applied on:
ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This way you deque a Cell from your own tableView and not from the one created by UISearchDisplayController.
It seems that nothing in your code forces searchDisplayController to reload it's searchResultTableView.
The standard approach is to set UISearcDisplayController's delegate (note the protocol - UISearchDisplayDelegate) and implement – searchDisplayController:shouldReloadTableForSearchString:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// perform your search here and update self.searchResults
NSInteger searchStringLength = searchString.length;
for (ListingPreview *listingPreview in self.listingPreviews) {
if ((listingPreview.title.length >= searchStringLength) && ([listingPreview.title rangeOfString:searchString].location != NSNotFound)) {
[self.searchResults addObject:listingPreview];
}
}
// returning YES will force searchResultController to reload search results table view
return YES;
}