Select row programmatically


#1

How can i programmatically select a row, i want to make the first row selected when the grid load. i cannot example code for this. 

thank you.

ariel


#2

Greetings Program!

This should do the trick:

if ([[shinobiDataGrid visibleRows] count] > 0) {
	[shinobiDataGrid setRow:[[shinobiDataGrid visibleRows] objectAtIndex:0] asSelected:YES animated:YES];
}

Wg


#3

Nice Wizgod it works, i tried deselecting the row zero but no luck, how can i deselect the row zero when i selected another row?  


#4

 [_shinobiDataGridclearSelectionWithAnimation:YES];

works. :smiley:


#5

Wizgod,

i thought programmatically selecting the row can help me associate the object  (UITextField )inside the cell but seems not possible.  i want to know if there is a way to programmatically associate the row to its object when i clicked it.

image1


#6

i tried this workaround when i create the uitextfield i assigne an accesibilityhint text to be my referrence when i clicked it.

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
    {
        // both columns use a SDataGridTextCell, so we are safe to perform this cast
        SDataGridTextCell* textCell = (SDataGridTextCell*)cell;
        
        // locate the stock
        DelEquipment *delequipment=_sortedData[cell.coordinate.row.rowIndex];
       
        textCell.textField.text=@"";
        
        UITextField*txtDropOff=(UITextField *)[cell viewWithTag:100];
        if (![cell.coordinate.column.title isEqualToString:@"Drop Off"] && txtDropOff!=nil) {
            [txtDropOff removeFromSuperview];
        }
        
        UITextField*txtPickUp=(UITextField *)[cell viewWithTag:101];
        if (![cell.coordinate.column.title isEqualToString:@"Pick Up"] && txtPickUp!=nil) {
            [txtPickUp removeFromSuperview];
        }
        
        
        if ([cell.coordinate.column.title isEqualToString:@"Equipment Name"]) {
            textCell.textField.text=delequipment.EquipmentName;
            
        }
        
        if ([cell.coordinate.column.title isEqualToString:@"Drop Off"] && ! (cell.coordinate.row.rowIndex==summaryLineIndex)) {
            
            
            if (txtDropOff==nil) {
                txtDropOff=[[UITextField alloc]init];
                txtDropOff.tag=100;
               
                [txtDropOff setAccessibilityHint:[NSString stringWithFormat:@"%d",rowSelected]];
                 rowSelected=rowSelected+1;
           
                txtDropOff.delegate=self;
                txtDropOff.text=delequipment.DropOffs;
                txtDropOff.borderStyle=UITextBorderStyleRoundedRect;
                txtDropOff.frame=CGRectMake(12, 4, 110, 28);
                txtDropOff.textAlignment=UITextAlignmentRight;
               
                NSLog(@"row select %d", rowSelected);
                        [txtDropOff addTarget:self action:nil forControlEvents:UITextFieldTextDidBeginEditingNotification];
            [textCell addSubview:txtDropOff];
            
        }
        
        
    }




-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    
    if (self.pop) {
        [self.pop dismissPopoverAnimated:YES];
    }
    
    if (textField.tag==100 ) {
        
        NSInteger actualRow=[textField.accessibilityHint integerValue];
        [_shinobiDataGrid clearSelectionWithAnimation:YES];
      
        if ([[_shinobiDataGrid visibleRows] count] > 0) {
            [_shinobiDataGrid setRow:[[_shinobiDataGrid visibleRows] objectAtIndex:actualRow] asSelected:YES animated:YES];
        }
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        Keypad *controller = [storyBoard instantiateViewControllerWithIdentifier:@"Keypad"];
        controller.delegate=self;
        UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:controller];
        popover.delegate = self;
        popover.popoverContentSize = CGSizeMake(250,300);
        rowIndicator=1;
        self.pop=popover;
        [popover presentPopoverFromRect:textField.bounds inView:textField permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        
    }

#7

I’m confused as to what you want to do; when begining editing you are programmatically selecting another row. Is that row the one you want to edit? What are the steps that you want to happen?

Wg


#8

What if I would like to select non visible row? If in my grid there are 30 rows, first 15 are visible and I should to select 20th?  :rolling_eyes:


#9

Greetings Program!

What you will have to do is check which row is being displayed in the prepareCellForDisplay method and select it there.

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
{
	int rowIndex = (int)cell.coordinate.row.rowIndex;
	
	if (rowIndex == myRowIwantToSelect) {
		[shinobiDataGrid setRow:cell.coordinate.row asSelected:YES animated:YES];
	}
}

Wg