Re: Re: How to set focus on editable table view cell on Enter key press
"Abhijeet Singh" <[email protected]> Wed, 05 Jan 2011 18:39:42 +0530
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
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)itemnum]; [wltable selectRowIndexes:iset byExtendingSelection:NO]; [wltable editColumn:(NSInteger)1 row:(NSInteger)itemnum 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 ke
y. 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 suggestAbhijeet Original message From:"Christiaan Hofman"< [email protected] >Date: 5 Jan 11 16:01:12Subject: Re: How to set focus on editable table view cell on Enter key pressTo: Cc: "macosxdev" On Jan 5, 2011, at 8:35, Abhijeet Singh 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. Co
lumn 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];elsereturn YES;}return NO;} (void)delete:(id)sender {if ([self canDelete])[[self delegate] tableView:self deleteRowsWithIndexes:[self selectedRowIndexes]];elseNSBeep();}ChristiaanGet Yourself a cool, short @in.com Email ID now!