How to get elements inside a selected row in a NSTableView? - objective-c

I have an NSTableView of content type View Based with two columns.
My objects are structured as follows:
Table View
Table Column
Table Cell View
Text Field
Table Column
Table Cell View
Popup Button
For a selected row, I want to get an element inside the selected row for a specific column. Specifically, I want to get the Text Field in the first column for the selected row.
I have an outlet to the table view and I have the index of the selected row so far, but that's about it:
- (void)getSelectedTextField
{
NSInteger selected = [tableView selectedRow];
}
Any ideas on how I can go about tackling this problem?
Edit: This is what I am trying to do: I want to change the text field to be in an editing state and focus on it so the user can start editing the text field value as soon as it is selected

I seemed to have solved my own problem:
- (void)getSelectedTextField
{
NSInteger selected = [tableView selectedRow];
// Get row at specified index
NSTableCellView *selectedRow = [tableView viewAtColumn:0 row:selected makeIfNecessary:YES];
// Get row's text field
NSTextField *selectedRowTextField = [selectedRow textField];
// Focus on text field to make it auto-editable
[[self window] makeFirstResponder:selectedRowTextField];
// Set the keyboard carat to the beginning of the text field
[[selectedRowTextField currentEditor] setSelectedRange:NSMakeRange(0, 0)];
}

Use can get all element of table view cell by get its subview. Suggest show log list subview before use it
NSInteger index = [self.tableViewFiles selectedRow];
// Get row at specified index of column 0 ( We just have 1 column)
NSTableCellView *cellView = [self.tableViewFiles viewAtColumn:0 row:index makeIfNecessary:YES];
// Get row's checkBox ( at index 0, textFields is 1,2)
NSButton *checkBox = [[cellView subviews] objectAtIndex:0];

If the text field is the only subview of your table view cell, you can get a reference to it with the following code, and select its text, so it's ready for editing:
-(void)tableViewSelectionDidChange:(NSNotification *)notification {
NSInteger row = [notification.object selectedRow];
NSTextField *tf = [[[notification.object viewAtColumn:0 row:row makeIfNecessary:NO]subviews] lastObject];
[tf selectText:tf.stringValue];
}

Related

how to solve this uitablview cell uitextfield focus issues

i have created ios app. uitableview each row show on textbox. button click to move next row. default show on 10 rows. last record to scroll to another records.
here is the code
if (![self.tableView.indexPathsForVisibleRows containsObject:indexPath]) {
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionMiddle animated:YES];
self.nextRow = i;
self.nextColumn = j;
}
now scroll the table but cursor focus on last records(10). how to focus set on 11th records. here is the attachment
Try TPKeyboardAvoidingTableView class for your tableView. It will manage this by itself.

Make cells appear and disappear in TableView

I making an app with a table view and a data source (core data). In this table i group several tasks ordered by date, and i have this segmented control.
I want the table to only load the tasks later or equal than today's date, when the user taps the second segment i want to show all tasks, if he taps the first segment the table must only show the later dates tasks again.
The problem is:
1 - I'm using fetchedResultsController associate with a indexPath to get the managed object.
2 - I use the insertRowsAtIndexPaths:withRowAnimation: and deleteRowsAtIndexPaths:withRowAnimation: methods to make the cells appear and disappear. And this mess with my indexPaths, if i want to go to the detail view of an specific row it is associate with a different indexPath, after delete the rows.
This problem was fixed by a method i did, but i still have other problems of indexPaths and cells, and it seems to me that is gone be me messy to each problem a fix.
There is a simple way to do that?
I tried just to hide the cells instead of delete, it works just fine, but in the place of the hidden cells was a blank space, if there is a way to hide these cells and make the non-hidden cells occupy the blank space i think that will be the simplest way.
Anyone can help me?
set the height of the cell to 0 when it hides, and set the height back to the original value when it appears.
TableViewController.h
#interface TableViewController{
CGFloat cellHeight;
}
TableViewController.m
- (void)cellHeightChange{
//if you need hide the cell then
cellHeight = 0;
cellNeedHide.hidden = YES;
//if you need hide the cell then
cellHeight = 44; // 44 is an example
cellNeedHide.hidden = NO;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
switch (section) {
// for example section 0 , row 0 is the cell you wanna hide.
case 0:
switch (row) {
case 0:
return cellHeight;
}
}
}
When the user taps on a segment execute a new fetch request on your managed object to give you an appropriate array (either an array of all dates, or the greater/equal dates). Then use reloadData on the tableView using this new array in the datasource.
or
Give the cell's you wish to hide a height of 0?

xcode picker output into array

I have table cell view with which each cell must represent the output of a picker selection by the user.
The table cell view has two labels and a button. The button is to call up the picker of course.(little right arrow)
The first label i need to return the number of the cell row?
The second label needs to return the picker selection of the user.
This is what i have in my picker implementation method file for the button upon picker selection by the user:
-(IBAction)buttonPressed
{
NSInteger suitRow = [doublePicker selectedRowInComponent:
kSuitComponent];
NSInteger numberRow = [doublePicker selectedRowInComponent:
kNumberComponent];
NSString *suit = [suitTypes objectAtIndex:suitRow];
NSString *number = [numberTypes objectAtIndex:numberRow];
This is where i need to put suit and number into array?
i also want to return the row number?
}
So i need to save those two values and return them to my other view?
Presumably the object in your array is already created since you are editing an existing row. So you should pass this object to your picker view controller, and edit it in the action above. When you return to the table view you may need to call reloadData
It may be worth looking at editing your cells in the same view, using the picker as the editing view of the field(s) in your cell. This would keep everything on the same controller and simplify your code as you'd know which item you were editing.

Retrieving cell contents from NSOutlineView

I have an odd issue cropping up with an NSOutlineView. The view is essentially a list of apps with associated files as children. I populate the view by hand in it's data source and all of that works fine. What I now want to do is have a button to remove an item. In order to do it I implemented a method, removeAppOrFile, like so:
- (IBAction)removeAppOrFile:(id)sender
{
NSInteger selectedRow = [myView selectedRow];
if (selectedRow == -1) //ie. nothing's selected
{
return;
}
NSTableColumn *col = [myView tableColumnWithIdentifier:#"Column 1"];
NSCell *cell = [col dataCellForRow:selectedRow];
NSString *item = [cell stringValue];
NSLog(#"The row is: %ld\nThe column is: %#\nThe cell is: %#\nThe selected item is: %#",selectedRow, col, cell, item); // For testing purposes
}
myView is an IBOutlet connected to my NSOutlineView.
If I select a different row and click the button the value for selectedRow will change properly, but the NSCell object never changes, and what should be it's value (ie. NSString item) always shows that of the last visible item (ie. if there's an item with children as the last item NSString item will be the parent if it's not expanded, or the last child if it is expanded).
The weird thing is that I use basically the same code elsewhere for a doubleAction on an NSOutlineView and it works perfectly. In that case, the code is as below:
- (void)editedAppOrFile:(id)sender
{
NSInteger rowNumber = [sender clickedRow];
NSTableColumn *col = [sender tableColumnWithIdentifier:#"Column 1"];
NSCell *cell = [col dataCellForRow:rowNumber];
NSString *item = [cell stringValue];
NSLog(#"The row is: %ld\nThe column is: %#\nThe cell is: %#\nThe selected item is: %#",selectedRow, col, cell, item); // For testing purposes
}
In this case sender is the outlineView. item & cell change with rowNumber change.
Any idea as to why it's not working in the first example?
There are a few issues with your approach.
You're getting the data cell, not the -preparedCellAtColumn:row:, so you have no guarantees about what its internal object value will be.
You can ask the outline view directly for -itemAtRow:.
If you're trying to remove (in the first case) or edit (the second case), you really only need to modify your data source and then note number of rows changed (first case) or reload data for row (second case).

How can I select a cell without touching the screen?

I have a UITableViewController and want to move down a cell in my table every time the accelerometer's X axis is greater then 0.5 (when this event occurs i increment a value named "TEST" ). How can i change the background of a cell that has it's indexPath.row equal to TEST ?
Here is how i try to access the method(from the accelerometer function) but it gives me an error :
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if(acceleration.x>0.5)
TEST++;
if(TEST<0) TEST=0;
if(TEST>19) TEST=19;
NSIndexPath *temp = [[NSIndexPath alloc] initWithIndex:TEST];
[self tableView:[self tableView] didSelectRowAtIndexPath:temp];
}
I have 20 rows in my table (hence the clamp).
selectRowAtIndexPath:animated:scrollPosition:
Selects a row in the receiver identified by index path, optionally scrolling the row to a location in the receiver.
(void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Parameters
indexPath
An index path identifying a row in the receiver.
animated
YES if you want to animate the selection and any change in position, NO if the change should be immediate.
scrollPosition
A constant that identifies a relative position in the receiving table view (top, middle, bottom) for the row when scrolling concludes. See “Table View Scroll Position”
here
   NSIndexPath *temp = [[NSIndexPath alloc] initWithIndex:TEST];
[self.tableview selectRowAtIndexPath:temp animated:YES scrollPosition:UITableViewScrollPositionNone];
How about scrollToRowAtIndexPath:atScrollPosition:animated:?