ShinobiDataGrid remove column


#1

hi…

is there a way to remove shinobidatagrid column using this action… "select column using longPress ant then drag column down "… ??

(  we drag and remove minimized ios app in iPhone like this)  can anyone give solution for me… :neutral_face: ??


#2

Greetings Program!

This isn’t perfect but it’s a start.

To the RowAndColumnReordering sample app add the following:

// Add the UIGestureRecognizerDelegate
@interface ViewController () <SDataGridDelegate, SDataGridDataSource, UIGestureRecognizerDelegate>

// Add a property for the grid.
@property ShinobiDataGrid *shinobiDataGrid;

// Add a property for the starting location of the long press gesture.
@property CGPoint startLocation;

@synthesize shinobiDataGrid;

Change:

// Create a grid that occupies the entire view
    ShinobiDataGrid *shinobiDataGrid = [[ShinobiDataGrid alloc] initWithFrame:CGRectInset(self.view.bounds, 62, 20)];

// Change to:

    shinobiDataGrid = [[ShinobiDataGrid alloc] initWithFrame:CGRectInset(self.view.bounds, 62, 20)];

Add:

- (void)viewDidLoad {
   [super viewDidLoad];

    // Add the gesture recognizer.
    UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    gestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:gestureRecognizer];

...

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    // Make sure it recognizes any other gestures (I.E. the long press for row/column reordering).
    return YES;
}

-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer
{
// NSLog(@"Long Press: %d", gestureRecognizer.state);

    CGPoint location = [gestureRecognizer locationInView:[shinobiDataGrid.subviews objectAtIndex:0]];
    
    switch (gestureRecognizer.state) 
    {
        case UIGestureRecognizerStateBegan:
        {
	    self.startLocation = location;
            NSLog(@"Start Location: %f, %f", location.x, location.y);
            break;
        }
        case UIGestureRecognizerStateChanged:
        {
            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"End Location: %f, %f", location.x, location.y);

            SDataGridColumn *selectedColumn = [self getColumnFromLocation:location];
            NSLog(@"Selected Column: %@", selectedColumn.title);
            
            float difference = abs(self.startLocation.y - location.y);
	    float tolerance = 10; // Default tolerance.
			
	    if (location.y < 0)
	    {
		// Dragging up above the boundries of the grid needs a lower tolerance.
		// I.E. In case the start location is the header.
		tolerance = 10;
	    }
	    else if (location.y < self.startLocation.y || location.y > self.startLocation.y)
	    {
		// Dragging up or down from anywhere else in the column needs a higher tolerance.
		//
		// Up and down dragging from anywhere in the column will only work if
		// row reordering is disabled.
		//
		// If row reordering is enabled, you will need to drag up/down from the header.
		tolerance = 50;
	    }

	    // Remove the column if the start and end location difference is greater than the tolerance.
	    // If row reordering is enabled, the starting point must be the header.
	    if (difference > tolerance && (shinobiDataGrid.canReorderRows == NO || (shinobiDataGrid.canReorderRows == YES && self.startLocation.y >= 0 && self.startLocation.y <= selectedColumn.headerCell.frame.size.height)))
	    {
		// Change the frame width of the grid to the total width of all remaining columns.
		[shinobiDataGrid setFrame:CGRectMake(shinobiDataGrid.frame.origin.x, shinobiDataGrid.frame.origin.y, shinobiDataGrid.frame.size.width-selectedColumn.headerCell.frame.size.width, shinobiDataGrid.frame.size.height)];

		// Remove the column.
		[shinobiDataGrid removeColumn:selectedColumn];

                // Reload the grid.
                [shinobiDataGrid reload];
            }
            break;
        }
        default:
            break;
    }
}

- (SDataGridColumn *)getColumnFromLocation:(CGPoint)location
{
    float runningWidth = 0;
    SDataGridColumn *column = nil;

    // The visible columns in the grid.
    NSArray *visibleColumns = shinobiDataGrid.visibleColumns;
    
    if (visibleColumns.count > 0)
    {
        // Loop through the columns, getting the column width and seeing if the location
        // is within the width of the column.
        for (SDataGridColumn *visibleColumn in visibleColumns)
	{
            NSLog(@"Visible Column: %@ (%f width)", visibleColumn.title, visibleColumn.headerCell.contentFrame.size.width);
			
            runningWidth += visibleColumn.headerCell.contentFrame.size.width;
            NSLog(@"Running Width: %f", runningWidth);

            // The location is within the boundries of this column.
            if (location.x <= runningWidth)
            {
                column = visibleColumn;
                break;
            }
        }
    }
	
    NSLog(@"Column at location: %@", column != nil ? column.title : @"No column found.");
	
    return column;
}

Wg


#3

nice solution wizgod… :laughing:


#4