I have a UICollectionView that is made up of a custom UICollectionViewCell subclass. The cell's are displaying correctly and are responding correctly to user's touches by firing this method:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
However, I am of the understanding that when a user touches the cell, it should highlight (in blue) and then the highlight should go away when the user lifts their finger. This is not happening. Any thoughts on why?
Here is some relevant code:
In the UICollectionView's datasource:
@implementation SplitCheckViewCollection
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"ReceiptCellIdentifier";
SplitCheckCollectionCell *cell = (SplitCheckCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.cellName.text = [NSString stringWithFormat:@"%@%i",@"#",indexPath.row+1];
return cell;
}
In the UICollectionViewCell's implementation:
@implementation SplitCheckCollectionCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SplitCheckCollectionCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
UICollectionViewCell: This is similar to a UITableViewCell in a table view. These cells make up the view’s content and are subviews to the collection view. You can create cells programmatically or inside Interface Builder.
One of the best things about UICollectionView is, like table views, it’s easy to set up collection views in the Storyboard editor visually. You can drag and drop collection views into your view controller and design your cell’s layout from within the Storyboard editor.
Implement UICollectionView Delegate method didSelectItemAtIndexPath to handle an event when the user taps on one of the collection view items Below is a very simple code example in Swift that creates UICollectionView programmatically. // Dispose of any resources that can be recreated.
UICollectionViewCell doesn’t allow for much customization beyond changing the background color. You’ll almost always want to create a subclass to easily access any content subviews you add. Choose File ▸ New ▸ File. Then choose iOS ▸ Source ▸ Cocoa Touch Class. Click Next.
The class only tells you about the highlight state, but doesn't change the visual appearance. You'll have to do it programmatically by e.g. changing the background of the cell.
Details are described in the CollectionView Programming Guide.
As SAE said,you have to do it yourself in a subclass. The other snag I just ran into is that when tapping a cell, it was receiving the highlight and redrawing if the cell was pressed and held. However, if tapped fast the redraw never happened.
I had created the cell in storyboard and the collection view has 'delays content touches' ticked as a default. I unticked this and it displayed instantly the finger touched the screen.
I am using a custom draw routine which checks the isHighlighted value. You also need to override setHighlighted in the custom cell as below or the draw routine never gets called.
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
There are 2 delegate methods you should to implement:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
Full code of highlighting collection view cell with animation:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor colorWithRed:232/255.0f green:232/255.0f blue:232/255.0f alpha:1]];
}
completion:nil];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor clearColor]];
}
completion:nil ];
}
You need to implement the UICollectionViewDataSource if you want to have Highlight and Unhighlight effect upon touch and release touch
here is the sample code
#pragma mark - UICollectionView Datasource
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:235/255.0f green:236/255.0f blue:237/255.0f alpha:.5];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = nil;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With