How add shinobigrid column Dynamically


#1

hello, i have implemented data grid.there is a menu that contain column names… what I want to do is select menu item and then that menu iteam should add my data grid… how do I do this…  :cry:


#2

Greetings Program!

When you select the menu item, simply add the column as you would when setting up the grid initially and then do a reload.

// Add a new column
    SDataGridColumn* column = [[SDataGridColumn alloc] initWithTitle:@"New"];
    column.width = @100;
    
    // Set the displayIndex if you want it inserted at a specific position, otherwise it will be added to the end.
    // If a column already exists in the position, it will be added after it. In this case, it will be added after
    // the first column becoming column 2 with a displayIndex of 1 and the diplayIndexes of the following columns
    // will be reordered (2, 3, ...). To add it as the first column, set the displayIndex to -1.
    column.displayIndex = 0;
    
    [shinobiDataGrid addColumn:column];
    
    [shinobiDataGrid reload];

Wg


#3

thanx for the reply wizgod… this is my first work experience with shinobigrid. I understand some part u said…but I’m confused… :blush: :blush:  can u give direct answer for my problem bellow…

this is how i add one column…

SDataGridColumn *vlumeColumn = [[SDataGridColumn alloc] initWithTitle:@"Volume"
    forProperty:@"volume"];
    sectorColumn.width = @150;
    [shinobiDataGrid addColumn:volumeColumn];

this is me menu item array…

(void)showMenu:(UIButton *)sender

{

    NSArray *menuItems =

    @[

      [KxMenuItemmenuItem:@"Choose column"

                     image:nil
                    target:nil
                    action:NULL],

      [KxMenuItemmenuItem:@"Industry"

                     image:[UIImage imageNamed:nil]
                    target:self
                    action:@selector(pushMenuItem:)],

      [KxMenuItemmenuItem:@"Volume"

                     image:nil
                    target:self
                    action:@selector(pushMenuItem:)],......];

       KxMenuItem *first = menuItems[0];
       [KxMenushowMenuInView:self.viewfromRect:sender.frame  menuItems:menuItems];
}

 and this is push action…

- (void) pushMenuItem:(id)sender
{
    NSLog(@"%@", sender);
           
    // in my log this NSLog work fine.when i push sector iteam in menu gives correct answer. 
}

when I select “Industry” or any other menu iteam that column need to show in my grid…

I implemented that method like this, but it didn’t sucsess…

- (void) pushMenuItem:(id)sender
{
    
    ShinobiDataGrid *shinobiDataGrid;
    
    if ([sender isEqualToString:@"Industry"]) {
        SDataGridColumn *industryColumn = [[SDataGridColumn alloc] initWithTitle:@"Industry" forProperty:@"industry"];
        industryColumn.canReorderViaLongPress = YES;
        industryColumn.width = @280;
        [shinobiDataGrid addColumn:industryColumn];
        
        [shinobiDataGrid reload];
    }
       
}

can u give me proper solution for my problem… 

thank you…


#4

If the push action is correctly called and the sender is of type string as you’re checking for then it should work in there.

In pushMenuItem, you need to access the ShinobiDataGrid that you originally created, not set up a new one. It should be a property that you can access directly in pushMenuItem.

@interface ViewController ()

// Create a property for your grid instance.
@property (nonatomic, strong) ShinobiDataGrid *shinobiDataGrid;

@end

- (void) pushMenuItem:(id)sender
{
    if ([sender isEqualToString:@"Industry"]) {
        SDataGridColumn *industryColumn = [[SDataGridColumn alloc] initWithTitle:@"Industry" forProperty:@"industry"];
        industryColumn.canReorderViaLongPress = YES;
        industryColumn.width = @280;
        [self.shinobiDataGrid addColumn:industryColumn];
        
        [self.shinobiDataGrid reload];
    }     
}

Wg


#5

It didn’t work… :cry: this is the log…

Dec 16 11:19:19 Charithas-Mac-mini-3.local CreatingCustomCells[1017] <Error>: CGAffineTransformInvert: singular matrix.
Dec 16 11:19:19 Charithas-Mac-mini-3.local CreatingCustomCells[1017] <Error>: CGAffineTransformInvert: singular matrix.
2014-12-16 11:19:25.401 CreatingCustomCells[1017:60b] <KxMenuItem #0x8d70160 Industry>
2014-12-16 11:19:25.401 CreatingCustomCells[1017:60b] -[KxMenuItem isEqualToString:]: unrecognized selector sent to instance 0x8d70160
2014-12-16 11:19:25.405 CreatingCustomCells[1017:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[KxMenuItem isEqualToString:]: unrecognized selector sent to instance 0x8d70160'
*** First throw call stack:
(
	0 CoreFoundation 0x022961e4 __exceptionPreprocess + 180
	1 libobjc.A.dylib 0x019278e5 objc_exception_throw + 44
	2 CoreFoundation 0x02333243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
	3 CoreFoundation 0x0228650b ___forwarding___ + 1019
	4 CoreFoundation 0x022860ee _CF_forwarding_prep_0 + 14
	5 CreatingCustomCells 0x00006681 -[ViewController pushMenuItem:] + 113
	6 Foundation 0x0136994f -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 863
	7 Foundation 0x0136955c -[NSObject(NSThreadPerformAdditions) performSelectorOnMainThread:withObject:waitUntilDone:] + 159
	8 CreatingCustomCells 0x00007fd1 -[KxMenuItem performAction] + 193
	9 CreatingCustomCells 0x0000a460 -[KxMenuView performAction:] + 224
	10 libobjc.A.dylib 0x01939880 -[NSObject performSelector:withObject:withObject:] + 77
	11 UIKit 0x003dc3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
	12 UIKit 0x003dc345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
	13 UIKit 0x004ddbd1 -[UIControl sendAction:to:forEvent:] + 66
	14 UIKit 0x004ddfc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
	15 UIKit 0x004dd243 -[UIControl touchesEnded:withEvent:] + 641
	16 UIKit 0x0041bddd -[UIWindow _sendTouchesForEvent:] + 852
	17 UIKit 0x0041c9d1 -[UIWindow sendEvent:] + 1117
	18 UIKit 0x003ee5f2 -[UIApplication sendEvent:] + 242
	19 UIKit 0x003d8353 _UIApplicationHandleEventQueue + 11455
	20 CoreFoundation 0x0221f77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
	21 CoreFoundation 0x0221f10b __CFRunLoopDoSources0 + 235
	22 CoreFoundation 0x0223c1ae __CFRunLoopRun + 910
	23 CoreFoundation 0x0223b9d3 CFRunLoopRunSpecific + 467
	24 CoreFoundation 0x0223b7eb CFRunLoopRunInMode + 123
	25 GraphicsServices 0x032fa5ee GSEventRunModal + 192
	26 GraphicsServices 0x032fa42b GSEventRun + 104
	27 UIKit 0x003daf9b UIApplicationMain + 1225
	28 CreatingCustomCells 0x00002b8d main + 141
	29 libdyld.dylib 0x01e1a701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I get kxMenu source from github…       https://github.com/kolyvan/kxmenu

what should I do… ??


#6

I merged the kxMenu demo and the GettingStarted ShinobiDataGrid demo. You have to cast the sender to KxMenuItem in order to do what you want.

Also make sure that you have the QuartzCore.framework added to the project. In the kxMenu demo, I had to change the target version to 8.x (I didn’t try 7.x but it should work too).

- (void) pushMenuItem:(id)sender
{
    NSLog(@"%@", sender);
	
    KxMenuItem *menuItem = (KxMenuItem *)sender;

    NSLog(@"%@", menuItem.title);

    if ([menuItem.title isEqualToString:@"Industry"]) {
	SDataGridColumn *industryColumn = [[SDataGridColumn alloc] initWithTitle:@"Industry" forProperty:@"industry"];
	industryColumn.canReorderViaLongPress = YES;
	industryColumn.width = @280;
	[self.shinobiDataGrid addColumn:industryColumn];
	
	[self.shinobiDataGrid reload];
    }
}

Wg


#7

wisgod, again thanks for replying… again didn’t add new “Industry” column… :cry:.

I implement CreatingCustomCell project in shinobigrid…this is my implementation…can u correct me… :confused:

// AppDelegate.h
// CreatingCustomCells

#import <UIKit/UIKit.h>
@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@end

// AppDelegate.m
// CreatingCustomCells

#import "AppDelegate.h"
#import "ViewController.h"
#import <ShinobiEssentials/ShinobiEssentials.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    //self.window.rootViewController = [ViewController new];
    self.viewController = [[ViewController alloc] init];
    self.window.rootViewController = self.viewController;
    
    [self.window makeKeyAndVisible];
    return YES;
}
@end

// ViewController.h
// CreatingCustomCells
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

// ViewController.m
// CreatingCustomCells

#import "ViewController.h"
#import <ShinobiGrids/ShinobiDataGrid.h>
#import "StockPriceDataSource.h"
#import "PriceCell.h"
#import "KxMenu.h"
#import <ShinobiEssentials/ShinobiEssentials.h>

@interface ViewController () <SDataGridDataSourceHelperDelegate>

@property (nonatomic, strong) SDataGridDataSourceHelper* datasourceHelper;
// Create a property for your grid instance.
@property (nonatomic, strong) ShinobiDataGrid *shinobiDataGrid;
@end

@implementation ViewController{
 UIButton *_btn1;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [ShinobiDataGrids setLicenseKey:@""]; // TODO: add your trial license key here!

    // adding background image....
    UIImageView *iv = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [iv setImage:[UIImage imageNamed:@"ios-linen.png"]];
    iv.autoresizingMask = ~UIViewAutoresizingNone;
    [self.view addSubview:iv];
    

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_black.png"]];
        [headerView setFrame:CGRectMake((self.view.bounds.size.width-446) / 2, 0, 446, 92)];
        headerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [self.view addSubview:headerView];

    }
    
    //----------------------------------------------------------------
    
    _btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn1.frame = CGRectMake((self.view.bounds.size.width-700) / 2,(self.view.bounds.size.height-950) / 2, 100, 50);
    [_btn1 setTitle:@"Add Column" forState:UIControlStateNormal];
    [_btn1 addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn1];
    
    //-------------------------------------------------------------------
      
    // place grid correct position..
    ShinobiDataGrid *shinobiDataGrid = [[ShinobiDataGrid alloc] initWithFrame:CGRectMake(30, 120, self.view.bounds.size.width-60, self.view.bounds.size.height-160)];
    

    shinobiDataGrid.autoresizingMask = ~UIViewAutoresizingNone;
    
    
    //Freeze the first and second row of the grid
   // shinobiDataGrid.numberOfFrozenRows = 1;
    
    //Freeze the first and second columns in the grid
    shinobiDataGrid.numberOfFrozenColumns = 1;
      
    //---------------------------------------------------------------------
       
    // Add a name column
    SDataGridColumn *nameColumn = [[SDataGridColumn alloc] initWithTitle:@"Name"
    forProperty:@"name"];
    nameColumn.width = @240;
    nameColumn.sortMode = SDataGridColumnSortModeBiState;
    [shinobiDataGrid addColumn:nameColumn];
   
    // Add a sector column
    SDataGridColumn *sectorColumn = [[SDataGridColumn alloc] initWithTitle:@"Sector"
    forProperty:@"sector"];
    sectorColumn.width = @150;
    sectorColumn.displayIndex = 2;
    sectorColumn.canReorderViaLongPress = YES;
    sectorColumn.canResizeViaPinch = YES;
    [shinobiDataGrid addColumn:sectorColumn];
    
    // Add a bid price column
    SDataGridColumn *bidColumn = [[SDataGridColumn alloc] initWithTitle:@"Bid"
                forProperty:@"bidPrice"
                   cellType:[PriceCell class]
             headerCellType:[SDataGridHeaderCell class]];
    bidColumn.width = @150;
    bidColumn.sortMode = SDataGridColumnSortModeTriState;
    bidColumn.canReorderViaLongPress = YES;
    bidColumn.canResizeViaPinch = YES;
    [shinobiDataGrid addColumn:bidColumn];
    
    
    //-------------------------------------------------------------------------
    
    // Remove the gridlines
    shinobiDataGrid.defaultGridLineStyle = [SDataGridLineStyle styleWithWidth:0.0f withColor:[UIColor clearColor]];
    
    
    
 
    

  
 
   
    /*
    // select multiple celles in grid.......
    shinobiDataGrid.selectionMode = SDataGridSelectionModeCellMulti;
    */
     
    // enable re-ordering rows....
    shinobiDataGrid.canReorderRows = YES;
    
    // Create the helper
    self.datasourceHelper = [[SDataGridDataSourceHelper alloc] initWithDataGrid:shinobiDataGrid];
    self.datasourceHelper.delegate = self;
    
    // Supply the data to the helper
    self.datasourceHelper.data = [StockPriceDataSource getStockPrices];
    
    // Add to the view
    [self.view addSubview:shinobiDataGrid];
    
}


#pragma mark - SDataGridDataSourceHelperDelegate Methods

// The bid and ask columns use a custom cell - so we implement this delegate method in order to populate the cell
- (BOOL)dataGridDataSourceHelper:(SDataGridDataSourceHelper *)helper
                    populateCell:(SDataGridCell *)cell
                       withValue:(id)value
                     forProperty:(NSString *)propertyKey
                    sourceObject:(id)object {
    
    // For the bidPrice and askPrice properties we populate the cell ourselves
    if ([propertyKey isEqualToString:@"bidPrice"]) {
        StockPrice *stockPrice = (StockPrice *)object;
        PriceCell *priceCell = (PriceCell *)cell;
        priceCell.price = stockPrice.bidPrice;
        priceCell.priceMovement = stockPrice.bidPriceMovement;
        return YES;
    }
    
    if ([propertyKey isEqualToString:@"askPrice"]) {
        StockPrice *stockPrice = (StockPrice *)object;
        PriceCell *priceCell = (PriceCell *)cell;
        priceCell.price = stockPrice.askPrice;
        priceCell.priceMovement = stockPrice.askPriceMovement;
        return YES;
    }
    
    // Return 'NO' so that the datasource helper populates all the other cells in the grid.
    return NO;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)showMenu:(UIButton *)sender
{
    NSArray *menuItems =
    @[
      
      [KxMenuItem menuItem:@"Choose column"
                     image:nil
                    target:nil
                    action:NULL],
      
      [KxMenuItem menuItem:@"Industry"
                     image:[UIImage imageNamed:nil]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Volume"
                     image:nil
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Symbol"
                     image:[UIImage imageNamed:nil]
                    target:self
                    action:@selector(pushMenuItem:)],

      [KxMenuItem menuItem:@"more"
                     image:[UIImage imageNamed:nil]
                    target:self
                    action:@selector(pushMenuItem:)],
      ];
    
    KxMenuItem *first = menuItems[0];
    first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
    first.alignment = NSTextAlignmentCenter;
    
    [KxMenu showMenuInView:self.view fromRect:sender.frame
                                                        menuItems:menuItems];
    
}


- (void) pushMenuItem:(id)sender
{
    NSLog(@"%@", sender);
	
    KxMenuItem *menuItem = (KxMenuItem *)sender;
    
    NSLog(@"%@", menuItem.title);
    
    if ([menuItem.title isEqualToString:@"Industry"]) {
        SDataGridColumn *industryColumn = [[SDataGridColumn alloc] initWithTitle:@"Industry" forProperty:@"industry"];
        industryColumn.canReorderViaLongPress = YES;
        industryColumn.width = @280;
        industryColumn.displayIndex = 3;
        [self.shinobiDataGrid addColumn:industryColumn];
        
        [self.shinobiDataGrid reload];
    }
}

@end

please help me out… :confused:


#8

Add (so you don’t have to add “self.” to all references of shinobiDataGrid):

@synthesize shinobiDataGrid;

Change:

// place grid correct position..
    ShinobiDataGrid *shinobiDataGrid = [[ShinobiDataGrid alloc] initWithFrame:CGRectMake(30, 120, self.view.bounds.size.width-60, self.view.bounds.size.height-160)];

To (so you are using the shinobiDataGrid property when instantiating the grid):

// place grid correct position..
    shinobiDataGrid = [[ShinobiDataGrid alloc] initWithFrame:CGRectMake(30, 120, self.view.bounds.size.width-60, self.view.bounds.size.height-160)];

Wg


#9

thanx wizgod… u help me lot… :smile_cat: I’m going to buy ghinobigrid premium version… can u suggest me a related learning materials(free) such as post, ebooks…

Regards…  :smile:


#10

Check out the tutorials on the site and the demos that come in the package and when you get stuck trying to do something, ask in the forum. I don’t think the ShinobiSquad have scheduled visits to the forum so if you need quicker/direct support, you’ll have to buy their support package.

If you’re comfortable with using API docs, then that’s available as well.

Wg