Long press in DataGrid used for something other than reorder?


#1

Is it possible to set up a the DataGrid to listen for long presses and know which row/cell was pressed?  I am wanting to be able to long press rows and pop up a menu and not reorder the rows.


#2

Greetings Program!

Not sure if you can modify the gesture; you could always create a custom UILongPressGestureRecognizer but not sure how you’d get the pressed row.

In another way, you can implement it in:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid willEndReorderingRow:(SDataGridRow *)row
{
	NSLog(@"Long press on row: %ld", (long)row.rowIndex);

	// Pop up a menu.
}

I couldn’t find a “shouldReorderRow” delegate method so as an ugly work around in case the user does move the row, you could reload the grid here to restore the rows:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid didReorderRow:(SDataGridRow *)rowSwitched withRow:(SDataGridRow *)rowSwitchedWith
{
        // Reload the grid to restore the row order.
	[_shinobiDataGrid reload];
}

Wg


#3

Thanks.

Yeah, I can’t seem to find a clear way to get a cell or row from a custom UILongPressGesture.  

WillEndReorderingRow works pretty well for recognising the longpress, although I get a flicker that I’m not fond of when I reload the table.  This still may be the only real solution for now if you really need longpress.  I might just have to train the users and use the double tap gesture instead.


#4

See if only reloading the switched rows will limit the flicker:

@implementation ViewController
{
	SDataGridRow *switchedRow;
}

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid didReorderRow:(SDataGridRow *)rowSwitched withRow:(SDataGridRow *)rowSwitchedWith
{
	// Save the switched row.
	switchedRow = rowSwitched;
}

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid didEndReorderingRow:(SDataGridRow *)row
{
	// Reload only the two rows that were switched.
	[_shinobiDataGrid reloadRows:@[row, switchedRow]];
}

Wg