I'm creating a custom collectionViewCell called MessageCell. This message cell has three components, a headerLabel, messageContainerView, and footerLabel. The problem is that depending on the type of message (video, transaction, delivery confirmation, photo, text, etc) I want to display a specific type of view with specific actions etc.
What's the best way to accomplish this? I've tried setting up my container view as a UIView in my cell subclass, and depending on the type of message, set it equal to a specific subview but that isn't working:
- (void)setMessage:(EMKMessage *)message {
//Set Message
_message = message;
//Check Message Type
switch (message.type) {
case MessageTypeText:
default: {
//Create Message Content View
TextContentView *textContentView = [[TextContentView alloc] initForAutoLayout];
textContentView.frame = CGRectMake(0, 0, 300, 200);
[textContentView setText:message.text];
self.messageContainerView = textContentView;
break;
}
}
}
Any help would be greatly appreciated.
You can create separately all the cells you need. In
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
you can renturn different cells depending on your needs. Just check the type of object you want to represent at the indexPath and return the corresponding cell. Those cell can have delegates if you need to interract with them or you can use block properties. Something like:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
VideoMessageCell *cell = [tableView.dequeueReusableCellWithIdentifier:#"VideoMessageCell"];
//set the cell properties
return cell;
} else if (indexPath.row == 1) {
AudioMessageCell *cell = [tableView.dequeueReusableCellWithIdentifier:#"AudioMessageCell"];
//set the cell properties
return cell;
}
}
Now, I don't know how you decide which type of cell do you need for a given index but you can replace indexPath.row to suit your needs. Also do not forget to set reusable identifiers accordingly.
Related
I implemented a calendar view with UICollectionView, when scrolling the calendar view very fast, it's not smooth. So I'm thinking whether I can load static content of each cell firstly, and then refresh once specific content has been loaded. So how to delay loading specific contents of each UICollectionViewCell
Specifically, in below function, I'll construct each UICollectionViewCell and return it. Now I just want to construct static contents (such as the date), and delay loading specific contents (such as the background color, if I have an event this day, I'll change the background of this cell), so where should I load specific contents, and how to only refresh showing cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:UICollectionViewCellIdentifier
forIndexPath:indexPath];
NSDate *date = [self dateAtIndexPath:indexPath];
cell.dateLabel.text = [date description];
// This is the part I want to delay, since it's cost.
if (dataModel.hasEventAtDate(date)) {
cell.dateLabel.backgroundColor = [UIColor blue];
}
return cell;
}
You may need have an instance variable to track if cells need to update:
Boolean cellNeedsUpdate = NO
In cellForItemAtIndexPath, check if you need to fully update the cells:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (cellNeedsUpdate) {
// fully update the cell
} else {
// do partial update
}
}
Track end scrolling of the collectionView, then reload the collectionView:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
cellNeesUpdate = !cellNeedsUpdate;
[collectionView reloadData];
}
I have a UICollectionView that uses a couple of different layouts depending on user preferences. I want certain cells to show up for some layouts but not for others. How do I accomplish this? Do I actually need to reload the collection view data?
There are two steps to accomplishing this (which also works with UITableView).
Update the data source. If you're using an NSDictionary or NSArray, then you'll need to add or remove the items you want to show/hide.
Call reloadData on the UICollectionView or UITableView. That's pretty much it.
If you want to remove or add with an animation, that's different. There are a couple more methods in the middle that you have to call and make sure that your update sequence is correct. But that is a different question altogether.
EDIT:
As an example of how to use an array
- (void)methodCalledWhenLayoutChanges:(BOOL)includeOptionalString {
if (includeOptionalString) {
[_collectionViewDataSourceArray addObject:_optionalString];
} else {
[_collectionViewDataSourceArray removeObject:_optionalString];
}
[self.collectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _collectionViewDataSourceArray.count;
}
// Never actually setup a collection view like this. This is just an example of how to reference a data source for creating a collection view cell.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
UILabel *textLabel = [[UILabel alloc] initWithFrame:cell.bounds];
textLabel.text = [_collectionViewDataSourceArray objectAtIndex:indexPath.row];
[cell addSubview:textLabel];
return cell;
}
I have a collectionView, where each 5th cell must be different than others.
i have write the following code:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.row + 1) % 5 == 0) {
return CGSizeMake(screenRect.size.width, screenRect.size.height/5);
}
return CGSizeMake(screenRect.size.width/2, screenRect.size.height/2);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ((indexPath.row + 1) % 5 == 0 ) {
NSLog(#"iAdCellWithIndex:%ld", (long)indexPath.row);
CustomCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
if (!cell)
cell = [[CustomCollectionViewCell alloc] init];
return cell;
}
ImageThumbCell * cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:#"ImageCell" forIndexPath:indexPath];
if (!cell1)
cell1 = [[ImageThumbCell alloc] init];
[cell1 setResultElement:intermediateResults_[indexPath.row]];
return cell1;
}
1-st cycle (first 5 elements) appear well, but then is loading 6-th element I get an exception :
MyApp[1503:888411] -[CustomCollectionViewCell resultElement]: unrecognized selector sent to instance 0x1a9882a0
I dont understand - Why ? the 6-th cell (cell at index 5) must be kind of ImageThumbCell class, not CustomCollectionView class.
can anyone explain this mistake ?
// sorry for bad english. i'm learning, honestly :)
// thanks
The code you posted is calling the setResultElement method, not resultElement. Thus that code shouldn't generate the crash you're seeing.
Your code for creating cells seems reasonable. On every 5th cell you try to dequeue a different type of cell, and if none are available, you alloc/init that other cell type. That makes sense.
My guess is that your bug is somewhere else in your code. Are you trying to read the value of your cell's resultElement property somewhere else?
I wonder which's the easiest way to have two different UICollectionViewCells-identifiers in the same UICollectionView? I've got an UISegmentedController that I want to switch between to different styles of UICollectionViewCells.. Where to implement which code. I've got an PatternViewCell for the first Cell but how do i do with the other? Please advice!
You can have two collection view cell prototypes registered for a single cell class for a single collection view with a single data source.
First, in your storyboard, set Cell1 as a reuse identifier for the first cell prototype and Cell2 for the second one. Both of them should have PatternViewCell class.
Then, on changing value of your segmented control you reload your collection view:
- (IBAction)segmentedControlValueChanged:(id)sender {
[self.collectionView reloadData];
}
Bind this action to your segmented control for the Value Changed event.
Then in - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method you can choose a reuse identifier depending on selected index.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = nil;
if (self.segmentedControl.selectedSegmentIndex == 0) {
identifier = #"Cell1";
} else {
identifier = #"Cell2";
}
PatternViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//configure cell
return cell;
}
I'm kinda newbie in UICollectionView and I'm working in a project that dynamically changes the UICollectionViewLayout in a given action.
My CollectionView has 6 sections, each of them with 10 elements. Those Cells are basically an UIImageView and my Custom Layout called StackViewLayout stacks all elements for each section (something like Apple's Photos.app).
If the user selects the element of the stack (for all sections), the UICollectionView dynamically changes the layout to the UICollectionViewFlowLayout, so all the elements can be viewed as grid.
My problem is that when user selects a stack, no matter which section, when the Layout is changed do Flow Layout, all sections are displayed in the grid, instead of displaying the elements for the selected section (stack), which is the behavior I wanted.
Is there any way to show only the Flow Layout for the selected section in the Custom Layout?
Here is my Controller Implementation snippet code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadStackLayout]; // stack all sections at first load.
}
#pragma mark - UICollectionView Data Source Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 6;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
// custom UICollectionViewCell, which will hold an image.
CVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.imageView.image = _images[indexPath.row];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// element size.
return CGSizeMake(100,100);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (isStacked)
{
[self loadFlowLayout]; //HERE I WANT TO LOAD FLOW LAYOUT ONLY FOR THE SECTION OF THE SELECTED ITEM!
} else
{
// to implement.
}
}
// HERE IS THE METHOD THAT CALLS MY CUSTOM LAYOUT IN ORDER TO STACK THE ELEMENTS FOR EACH SECTION IN COLLECTION VIEW. IT IS WORKING AS IT SHOULD.
-(void)loadStackLayout
{
if (([self.collectionView.collectionViewLayout isKindOfClass:[UICollectionViewFlowLayout class]]))
{
isStacked = YES;
[self.collectionView setCollectionViewLayout:[[StackViewLayout alloc] init] animated:YES];
}
}
// HERE IS THE METHOD THAT CALLS MY FLOWLAYOUT IN ORDER TO UN-STACK THE ELEMENTS AND SHOW THEM IN A GRID. CURRENTLY IT IS SHOWING ALL SECTIONS IN THE GRID.
-(void)loadFlowLayout
{
if (([self.collectionView.collectionViewLayout isKindOfClass:[StackViewLayout class]]))
{
isStacked = NO;
[self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init] animated:YES];
}
}
I think you can probably do it by having an if clause in your numberOfItemsInSection: method, to return 0 for any section that's not the selected one. Obviously, you'll need to keep track of the selected section to do this.