SDataGridHeaderCell subclass


#1

I am migrating to the new API.  I need the ability to respond when the user taps on the header cell.

Subclassing SDataGridHeaderCell doesn’t seem to work like regular cells.

I don’t see anything in samples which subclasses a header cell.

Any pointers for me?


#2

And why does Coordinate return null for subclasses of SDataGridHeaderCell?  Is there a way to find out which column I am in?


#3

Hi Erik,

You are correct that the SDataGridColumnHeaderCell currenlty overrides the coordinate property to return nil. I’ll have to chat with the team and review why this is the case - and potentially change this behaviour.

In the meantime, there are a couple of ways you could detect the column that a header cell belongs to. You could inspect the header text …

#import "CustomHeader.h"
#import <ShinobiGrids/SDataGridColumn.h>

@implementation CustomHeader
{
    UITapGestureRecognizer* _tap;
}

- (id)initWithReuseIdentifier:(NSString *)identifier
{
    if (self = [super initWithReuseIdentifier:identifier]) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
        [self addGestureRecognizer:_tap];
    }
    return self;
}

- (void) handleSingleTap
{
    NSLog(@"header %@", self.textField.text);
}

@end

Or, if your header text does not provide a unique identification, you can navigate the view hierarchy in order to find the woning data-grid, then iterate over the columns to determine whcih one owns the header cell:

#import "CustomHeader.h"
#import <ShinobiGrids/SDataGridColumn.h>

@implementation CustomHeader
{
    UITapGestureRecognizer* _tap;
}


- (id)initWithReuseIdentifier:(NSString *)identifier
{
    if (self = [super initWithReuseIdentifier:identifier]) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
        [self addGestureRecognizer:_tap];
    }
    return self;
}

- (void) handleSingleTap
{
    // this is not the most elegant way to traverse the view hierarchy!
    ShinobiDataGrid* dataGrid = (ShinobiDataGrid*)self.superview.superview.superview.superview.superview;
    
    SDataGridColumn* owningColumn;
    for (SDataGridColumn* col in dataGrid.columns) {
        if (col.headerCell == self) {
            owningColumn = col;
            break;
        }
    }
    
    NSLog(@"column %@", owningColumn);
}

@end

I’ll make sure we review our API to see whether we can make this easier in future.

Regards, Colin E.