DataGrid: how to retrieve coordinates for tap on section header


#1

Hi,

I need to retrieve the coordinates for the tap gesture on a section header.

I started looking to the method 

– shinobiDataGrid:shouldCollapseSectionAtIndex:

but i can’t figure out how to retrieve the tap location, nor the coordinates.

Any help?

Davide


#2

Nobody has an answar for that? :cry:


#3

Greetings Program!

What are you trying to do? Do you mean the actual screen tap coordinates or the grid coordinates?

There are no coordinates for section headers; the SDataGridCoord class is for the cell object.

The closest thing would probably be the coordinates for the first cell in the section:

- (BOOL)shinobiDataGrid:(ShinobiDataGrid *)grid shouldCollapseSectionAtIndex:(NSUInteger)sectionIndex
{
	SDataGridCoord *coord = [[SDataGridCoord alloc] initWithCol:[SDataGridColumn columnWithTitle:@"forename"] row:[SDataGridRow rowWithRowIndex:0 sectionIndex:sectionIndex]];
	NSLog(@"Coordinates of first cell at section: %@", coord);
	
	// I don't know why cell is null; maybe ShinobiSquad has an idea.
	/*
	SDataGridCell *cell = [grid visibleCellAtCoordinate:coord];
	NSLog(@"Cell at coordinates: %@", cell);
	*/
	
	return true;
}

Wg


#4

The problem is that i need the column information! :cry:


#5

I was playing around and came up with this ugly way of getting the column info (this is using the DataGridWithSections sample code):

  1. Add a gesture to the grid.

    // Create and initialize a tap gesture
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
    initWithTarget:self action:@selector(showGestureForTapRecognizer:)];
    // Specify that the gesture must be a single tap
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.delegate = self;

    // Add the tap gesture recognizer to the view
    [[_shinobiDataGrid.subviews objectAtIndex:0] addGestureRecognizer:tapRecognizer];

  2. Make sure all gestures are recognized.

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
      {
      // Make sure it recognizes any other gestures (I.E. the row/cell selection).
      return YES;
      }
  3. Find the column.

    • (IBAction)showGestureForTapRecognizer:(UITapGestureRecognizer *)recognizer {

      // Get the location of the tap.
      CGPoint location = [recognizer locationInView:[_shinobiDataGrid.subviews objectAtIndex:0]];

      NSLog(@“Gesture tap location: %f, %f”, location.x, location.y);

      float runningWidth = 0;
      SDataGridColumn *tappedColumn = nil;

      // The visible columns in the grid.
      NSArray *columns = _shinobiDataGrid.visibleColumns;

      if (columns.count > 0) {
      // Loop through the columns, getting the column width and seeing if the tap location
      // is within the width of the column.
      for (SDataGridColumn *column in columns) {
      NSLog(@“Column: %@ (%@ width)”, column.title, column.width);
      runningWidth += [column.width floatValue];

        // The location is within the boundries of this column.
        if (location.x <= runningWidth)
        {
        tappedColumn = column;
        break;
        }
      

      }
      }

      if (tappedColumn != nil)
      {
      NSLog(@“Tapped column: %@”, tappedColumn.title);
      }
      }

Hope it helps.

Wg


#6

Thank you,

I will give it a try as soon as I can!

Davide