Search code examples
iosgmgridviewgmgridviewcell

Cell highlighting for the GMGridView


I'm using the GMGridView with a subclassed UIView as the contentView of the cells. These are assigned in the

- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index

Now I'd like the cells to have a highlight, similar to the behavior of the UIButton. Meaning that there is a slightly opaque darker overlay. I just can't figure out where I'd have to assign the highlighting.

I saw that GMGridViewCell has a property called highlighted, which indicates if the cell ist highlighted or not. Setting it to cell.highlighted = YES; in the above method doesn't change anything in the appearance of the cell.

In the description on the GMGridView website it says

Features - General:

  • Cell highlighting support

So, there should be a way ...

Can anybody tell me, how the highlighting should be assigned?


Solution

  • I hate it when this happens. Just found the answer.

    Apparently the highlighting will be applied to the subviews of cell.contentView, which can be seen here in the commit.

    The cell will be set highlighted while touched:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.highlighted = YES;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.highlighted = NO;
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.highlighted = NO;
    }
    

    And then the subviews' highlight state will be activated.

    -(void)setHighlighted:(BOOL)aHighlighted {
        highlighted = aHighlighted;
    
        [self.contentView recursiveEnumerateSubviewsUsingBlock:^(UIView *view, BOOL *stop) {
            if ([view respondsToSelector:@selector(setHighlighted:)]) {
                [(UIControl*)view setHighlighted:highlighted];
            }
        }];
    }
    

    So, to realize cell highlighting, I just had to add an UIImageView with the transparent layer above the parts of my custom cell view. Hope this helps anyone.