gmoledina / GMGridView

A performant Grid-View for iOS (iPhone/iPad) that allows sorting of views with gestures (the user can move the items with his finger to sort them) and pinching/rotating/panning gestures allow the user to play with the view and toggle from the cellview to a fullsize display.
MIT License
2.31k stars 512 forks source link

Complex cells with various elements, using nib #112

Open cannyboy opened 12 years ago

cannyboy commented 12 years ago

Can anyone help with creating complex cells with GMGridView with various elements such as UIViews, UIimageViews, UIButtons etc. These elements may have unique graphics for each cell.

I'd like to be able to do it with a nib file, but any code would be useful.

cannyboy commented 12 years ago

This is my first attempt at making a nib-based grid.. It works, but I'm sure someone cleverer than me can make it better, faster, and prettier.

Make a subclass of GMGridViewCell, which loads in a named nib:

//  CustomGridCell.h
#import "GMGridViewCell.h"
@interface CustomGridCell : GMGridViewCell
@property (strong, nonatomic) IBOutlet UIButton *button;
@property (strong, nonatomic) IBOutlet UIView *customContentView;
+(CustomGridCell *)cellFromNibNamed:(NSString *)nibName;
@end

//  CustomGridCell.m
#import "CustomGridCell.h"
@implementation CustomGridCell
@synthesize button;
@synthesize contentView;
+(CustomGridCell *)cellFromNibNamed:(NSString *)nibName
{
    UIViewController *tempViewController = [[UIViewController alloc] initWithNibName:nibName bundle:nil];
    CustomGridCell *customCell = (CustomGridCell *)tempViewController.view;
    return customCell;
}
@end

A nib is created to work with this. You create it buy using a UIView. Connect the FileOwner's view to this view. Within the nib, change the main view's class to CustomGridView. Within its main view, you place another view, which covers all the main view. Link this view to customContentView. Now add other stuff on top. I added a button for instance.

After importing CustomGridView in the Demo1ViewController header, change the cellForItemAtIndex in Demo1ViewController.m to looks like this:

- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
    CustomGridCell *cell = (CustomGridCell*)[gridView dequeueReusableCell];
    if (!cell) 
    {
        cell = (CustomGridCell *)[CustomGridCell cellFromNibNamed:@"CustomGridCell"];
        [cell.button setTitle:[NSString stringWithFormat:@"button:%i", index] forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    }
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    return cell;
}

The property customContentView is used instead of contentView, so you can change the contentView to customContentView in other parts of Demo1ViewController if you want some effect to take place.

To get buttons (and other controls to work) I added this to GMGridView.m:

// discussion here: https://github.com/gmoledina/GMGridView/issues/68
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ( [touch.view isKindOfClass:[UIControl class]] ) return NO;
    return YES;
}
HrishiPol commented 9 years ago

Thanks a lot ... it really help me a lot.