Change UICollectionViewCell content and nib layout based on data - iphone

I have a UICollectionView that is displaying many UICollectionViewCells that I have subclassed as CardCell. I pass the variable "type" to the CardCell in - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath I want the CardCell class to be able to load a different Nib file depending on what type is passed in. The different types need to have different layouts.
The problem is I cannot figure out where to change this in my CardCell.m. I tried using - (void)prepareForReuse but that does not call unless the user is scrolling.

You should register each nib file you need in viewDidLoad, something like this (substituting the correct names for the nib file and the identifier):
[self.collectionView registerNib:[UINib nibWithNibName:#"RDCell" bundle:nil] forCellWithReuseIdentifier:#"FirstType"];
Then, in itemForRowAtIndexPath, test for type and return the correct type of cell:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (type = #"firstType") {
FirstCell *cell = (FirstCell *) [collectionView dequeueReusableCellWithReuseIdentifier:#"FirstType" forIndexPath:indexPath];
return cell;
}else{
SecondCell *cell = (SecondCell *) [collectionView dequeueReusableCellWithReuseIdentifier:#"SecondType" forIndexPath:indexPath];
cell.whatever .....
return cell;
}
}

Related

Collectionview doesn't show anything during runtime

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return getName.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[getName objectAtIndex:indexPath.row]];
return cell;
}
When I run the app then it doesn't show anything.what are the reasons behinds that
Mostly its tag issue cause as per your crash log
[UICollectionViewCell setImage:] means image is directly set to UICollectionViewCell not recipeImageView. so you have to check proper tag value..
so ones check tag value of image view..
otherwise check that you have register your collection view cell.

UITableView Content "Static cell" not working in iOS7

I have a problem in my storyboard.It is working fine but when i try to change the content property of UITableView it caused following error
Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I want to design a grouped tableview with static cell.Thanks in advance
Code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
if you use static cell, just comment all the UITableViewDatasourceDelegate method. so comment the following code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Wish this will help you!
Instead of using:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//-> This is used when using prototype cells (e.g) Dynamic TableView
Use:
UITableViewCell *cell = [super tableView:tableView
cellForRowAtIndexPath:indexPath];
//-> Since you chose Static TableCells you don't need to reuse instead use the cells you created in nib
I have encountered same issue, and I think I found the way to work this out.
Create a Controller extends from UITableViewController to wrap its controller, it will have default UITableViewDelegate and UITableViewDataSource methods.
Remove UITableView delegate and datasource
Delete default UITableViewDelegate and UITableViewDataSource methods from your controller file, coz it's static cells, so if these exist, will not appear
Hope it helps.
When using storyboard the tableview cell must be registered in the storyboard.That means cell must be added to the table and its identifier must be set.This identifier should be used in the code in cellForRowAtIndexpath
EDIT
In case of static cells the each purticular cells needed to be created in the nib and filled content via code or nib
Read this
Loading Table View Cells from a Storyboard
Excellent starter
You are not allowed to use static cells in a normal UITableView.
Instead you need to use UITableViewController to go with the static cells.
this and this may or might help.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [YourArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
Add this code in cellForRowAtIndexPath
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
else
{
UIView *subview;
while ((subview= [[[cell contentView]subviews]lastObject])!=nil)
[subview removeFromSuperview];
}

Change UILabel insider Header for a collection view

When someone selects a cell in a collection view (about 15 cells) inside the
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
method I am trying to change a label I have placed in the one header by using
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"Header" forIndexPath:indexPath];
UILabel *headerTitle=(UILabel *)[headerView viewWithTag:1];
headerTitle.text=#"test";
the tag and everything is set correctly, but it does not change. Any thoughts on where I'm going wrong?
You already have a header view, so dequeuing one creates another one which is not the one you're trying to change. There is no way to access a header view in a collection view -- there's no headerForSection: method, so you have to change the label through your data source. So, if you just have the one header, then you should have a string property, lets call it headerTitle, that you use to populate the label in the header. So, your implementation of collectionView:viewForSupplementaryElementOfKind:atIndexPath: should look something like this:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
RDReusableHeader *headerView;
if (kind == UICollectionElementKindSectionHeader){
headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"MyView" forIndexPath:indexPath];
headerView.label.text = self.headerTitle;
}
return headerView;
}
Then in didSelectItemAtIndexPath:, assign a new value to that property and call reloadData on the collection view.
self.headerTitle = #"This is the New Tilte";
[self.collectionView reloadData];

Error on collectionView dequeueReusableCellWithIdentifier

I am having trouble in converting a uitable into collectionview.
I have the following code but it has error on "collectionView dequeueReusableCellWithIdentifier"May I know what;s the problem?
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
// Return the number of rows in the section (the amount of items in our array)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [pictureListData count];
}
// Create / reuse a table cell and configure it for display
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UICollectionViewController *cell = [collectionView dequeueReusableCellWithIdentifier:CellIdentifier];
// Get the core data object we need to use to populate this table cell
Pictures *currentCell = [pictureListData objectAtIndex:indexPath.row];
You have the wrong method name. For UICollectionViews the method is:
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath
So you need to change
UICollectionViewController *cell = [collectionView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UICollectionViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

iOS 6.0 Storyboard: Set static cell selected

I have created 5 static cells in UITableViewController scene. Everything working fine but I am not able to set the particular cell selected! If it is prototype cell then its simple but how could I set static cell selected? Following code raises BAD memory access exception! I have done this based on discussion on apple thread but not sure what's wrong! Could someone help please.
- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section == _rating)
[tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryCheckmark;
}
Fixed this. I have to return UITableViewCell return type and have to used this method like below. It was recursive calling the methods and hence was failing.
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section == _rating)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
return cell;
}