DataGrid - Editable Header Cells


I could swear I saw an article about this before, but can’t for the life of me find it.  I need the header cells in a DataGrid to be editable.  The doc says outright that I’ll have to subclass SDataGridHeaderCell, but stops there.  Any help or a link to RTFM would be appreciated.




I knew I’d seen this before – because I asked the question before…  If it’s of help to anybody else, here it is.


#import <ShinobiGrids/ShinobiDataGrid.h>
#import "HeaderCoordUtility.h"

@interface EditableHeaderCell : SDataGridHeaderCell <UITextFieldDelegate>



#import "EditableHeaderCell.h"

@implementation EditableHeaderCell
    UITextField *editableField;

- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    return self;

- (void) actUponEditEvent
    editableField = [[UITextField alloc] initWithFrame:self.textField.frame];
    editableField.autoresizingMask = ~UIViewAutoresizingNone;
    editableField.font = self.textField.font;
    editableField.text = self.textField.text;
    editableField.textColor = self.textField.textColor;
    editableField.delegate = self;
    editableField.contentVerticalAlignment = self.textField.contentVerticalAlignment;
    self.textField.hidden = YES;
    [self addSubview:editableField];
    [editableField becomeFirstResponder];

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.textField.text = textField.text;
    [textField removeFromSuperview];
    editableField = nil;
    self.textField.hidden = NO;
    NSUInteger colIndex = [HeaderCoordUtility findColumnOfHeaderCell:self];
    [HeaderCoordUtility setTitle:self.textField.text forColumnIndex:colIndex forHeaderCell:self];
    NSDictionary *cellInfo = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",colIndex] forKey:@"HeaderCellIndex"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataHeaderCellUpdated" object:self userInfo:cellInfo];


#import <Foundation/Foundation.h>
#import <ShinobiGrids/SDataGrid.h>

@interface HeaderCoordUtility : NSObject

+(void)setTitle:(NSString*)title forColumnIndex:(int)index forHeaderCell:(SDataGridHeaderCell*)cell;



#import "HeaderCoordUtility.h"

@implementation HeaderCoordUtility

+(NSUInteger)findColumnOfHeaderCell:(SDataGridHeaderCell *)cell
    ShinobiDataGrid *grid = [self findGridForHeaderCell:cell];
    // Loop through the cell's parent grid's column array until we find the cell.
    for (SDataGridColumn *column in grid.columns)
        if(column.headerCell == cell)
            // Use the index of the column in the grid's column array as our indication of the columns index.
            return [grid.columns indexOfObject:column];
    // No column found - I can't think of a situation where this would happen.
    return -1;

+(ShinobiDataGrid *)findGridForHeaderCell:(SDataGridHeaderCell *)cell
    // Loop through super views of the cell until we find a grid (which will always be the parent grid!).
    UIView *ancestorView = cell.superview;
    while (![ancestorView isKindOfClass:[ShinobiDataGrid class]])
        ancestorView = ancestorView.superview;
    return (ShinobiDataGrid*)ancestorView;

+(void)setTitle:(NSString*)title forColumnIndex:(int)index forHeaderCell:(SDataGridHeaderCell*)cell
    ShinobiDataGrid *grid = [self findGridForHeaderCell:cell];
    [grid.columns[index] setTitle:title];