Re: How to set focus on editable table view cell on Enter key press

Christiaan Hofman <[email protected]> Wed, 5 Jan 2011 18:46:24 +0100
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Jan 5, 2011, at 14:09, Abhijeet Singh wrote:

> 
> Hi,
> I am able to set the focus on editable cell using selectRowIndexes:byExtendingSelection: and -editColumn:row:withEvent:select:. I have a "addRow" method. I have written this code at the end of addRow method so that when ever new row is added the editable cell of new row get selected.
> I call addRow method after accepting the user input in - (void)tableView:(NSTableView *)pTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)pTableColumn row:(int)pRowIndex mehtod on Enter key press. This is how it looks.
> -(int)addRow
> {
> ....
> ....
> NSIndexSet *iset = [NSIndexSet indexSetWithIndex:(NSUInteger)item_num];
> 	[wltable selectRowIndexes:iset byExtendingSelection:NO];
> 	[wltable editColumn:(NSInteger)1 row:(NSInteger)item_num withEvent:nil select:NO];
> }
> - (void)tableView:(NSTableView *)pTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)pTableColumn row:(int)pRowIndex
> {	
> ....
> ....
> [self addRow];
> }
> It works fine if user enters some text in the table view cell and presses Enter key. But if user does not enter anything and just hit Enter key a new row is not added because tableView:setObjectValue:forTableColumn:row: mehtod is not invoked. So I removed the addRow call from tableView:setObjectValue:forTableColumn:row: and call it from NSControlTextDidEndEditingNotification. Something like this
> - (void)cellTextDidEndEditing:(NSNotification *)notif
> {
> 	[self addRow];
> }
> It adds a new row on Enter button click but now the editable cell of new row is not selected by default and the row color is also grey (is that mean that the focus is somewhere else?). How to fix this? Please suggest
> 
> Abhijeet
tableView:setObjectValue:forTableColumn:row: is used to change the value of an existing row. Adding a row when doing this is weird, and an abuse of the system. When you abuse something you should expect problems. We cannot help very much with telling you how to abuse it, as it will be wrong by definition. You should rethink the workflow for your app, as this does not make much sense. 

Christiaan
> ---------- Original message ----------
> From:"Christiaan Hofman"< [email protected] >
> Date: 5 Jan 11 16:01:12
> Subject: Re: How to set focus on editable table view cell on Enter key press
> To: 
> Cc: "macosx-dev" <[email protected]>
> 
> 
> On Jan 5, 2011, at 8:35, Abhijeet Sing h wrote:
> 
> > Hi,I have a table view in my application. Table view has 3 columns. Items (rows) in table view are added on Enter (return) key press on keyboard. Column no 2 is editable and user needs to fill it. What I want to achieve is when user press Enter key on key board:1. New row is added in table view with column 1 & 3 filled (by default)2. Column 2 cell should be in edit mode and ready to accept the user input I have done the first step (1) but i am not able to set the focus on column2 cell and make it editable on Enter key press.
> 
> Use -selectRowIndexes:byExtendingSelection: and -editColumn:row:withEvent:select:.
> 
> > One more thing that i want to do is on Delete key press on keyboard the selected row in table view should be deleted. I can do it by putting a button on screen and connecting it with Delete key but i don't want to use a button on my screen.Any help will be appreciated.Thanks & RegardsAbhijeetGet Yourself a cool, short @in.com Email ID now!
> 
> 
> For Delete, you need to implement -delete:, because that method is by default connected to the Delete key in the Edit menu. You implement that either in your window controller subclass, or in a tableview subclass, or both, depending on whether you want to have the delete key work everywhere or only when the tableview is focused. I have NSTableView subclasses that implement -delete: (and some other actions) going through custom delegate methods:
> 
> - (BOOL)canDelete {
> NSIndexSet *indexes = [self selectedRowIndexes];
> if ([indexes count] && [[self delegate] respondsToSelector:@selector(tableView:deleteRowsWithIndexes:)]) {
> if ([[self delegate] respondsToSelector:@selector(tableView:canDeleteRowsWithIndexes:)])
> return [[self delegate] tableView:self canDeleteRowsWithIndexes:indexes];
> else
> return YES;
> }
> return NO;
> }
> 
> - (void)delete:(id)sender {
> if ([self canDelete])
> [[self delegate] tableView:self deleteRowsWithIndexes:[self selectedRowIndexes]];
> else
> NSBeep();
> }
> 
> Christiaan
> 
> 
> 
> 
> Get Yourself a cool, short @in.com Email ID now!