NSTableView with Dropdown Menu and having Image inside Menu - objective-c

Yes, is it possible to have:
A table having two column ( Should be easy)
One of Cell should have image and that should be selectable from Drop - down menu
By googling what i came to know it has to be of type NSPopupButtonCell type, but i want only image inside it, no text,
How can i do that ?
The another column would be editable, user should be able to type in that.
it would be great if i can get any reference code to implement the same.

i did it with following way,
In Coloumn 1 select the DataCell and assign it of type NSPopupButtonCell, by default it wouldn't come, you need to select it explicitly.
In the Code add following lines of Code...
NSTableColumn *option = [pTableColumns objectAtIndex:[pTableView columnWithIdentifier:OPTION_COLUMN_NAME]];
NSTableColumn *shortCutItem = [pTableColumns objectAtIndex:[pTableView columnWithIdentifier:SHORTCUT_COLUMN_NAME]];
// we want first cell to have the Image & Menu
//Data type column drop down
NSPopUpButtonCell *dataTypeDropDownCell = [option dataCell];//[[NSPopUpButtonCell alloc] initTextCell:#"" pullsDown:YES];
[dataTypeDropDownCell setBordered:NO];
[dataTypeDropDownCell setEditable:YES];
NSArray *dataTypeNames = [NSArray arrayWithObjects:#"NULLOrignal", #"String", #"Money", #"Date", #"Int", nil];
[dataTypeDropDownCell addItemsWithTitles:dataTypeNames];
Add following code to set the correct MenuItem
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
if([[aTableColumn identifier] isEqualToString:OPTION_COLUMN_NAME]){
NSPopUpButtonCell *dataTypeDropDownCell = [aTableColumn dataCell];
[dataTypeDropDownCell selectItem:[ dataTypeDropDownCell itemAtIndex:3]];
}
}
Now only pending this is to append Image inside MenuItem which is not a big deal at all,
Again thanks for Looking at this, let me know if any other approach is there to do so....

Related

Populate NSTableView with unknown number of columns

I have a NSTableview and a button. NSTableview has a unknown number of columns.
The first column has a image well and 2 text boxes, the others (again, unknown number) are normal textbox columns.
The button opens up a file open dialogue. Once I choose the files (.jpg) I would like to process them.
So far everything is made (chose files, columns, etc..) what is missing is the populating of the table:
I have the loop that goes through all the selected files. What is the best way to do this:
display the image in the image well of the first cell,
type the filename in the first textbox of the first cell,
type the filepath in the second cell of the textbox,
type "YES" in all other columns.
My difficulty is that I have no idea how many columns will be there since it depends from the user. The number of columns will not change during Runtime. they are set up at startup based on the configuration. if the configuration is changed then the app should be reloaded.
I am a beginner in Objective-C/Cocoa programming.
EDIT:
additional info as requested:
It is a view based NSTableView
each column represents an action that has to be taken in a later moment on an image. the program user can decide what and how many actions to take, thats the reason for a unknown amount of columns in the table view.
You can add columns programmatically using addTableColumn:. This method takes an NSTableColumn instance that you can create in your code.
The rest of your architecture (displaying images, etc.) does not particularly change from "normal" code just because the columns have been added dynamically.
Here is a snippet that should get you started:
NSTableColumn* tc = [[NSTableColumn alloc] init];
NSString *columnIdentifier = #"NewCol"; // Make a distinct one for each column
NSString *columnHeader = #"New Column"; // Or whatever you want to show the user
[[tc headerCell ] setStringValue: columnHeader];
tc.identifier = columnIdentifier;
// You may need this one, too, to get it to show.
self.dataTableview.headerView.needsDisplay = YES;
When populating the table, and assuming that the model is an array (in self.model) of NSDictionary objects, it could go something like this;
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *columnIdentifier = tableColumn.identifier;
NSDictionary *rowDict = [self.model objectAtIndex: row];
NSString *value = [rowDict valueForKey: columnIdentifier]; // Presuming the value is stored as a string
// Show the value in the view
}
More in the docs.
When user adds a column or row, you should reflect it in your model (by binding or by code), so you know the size of your table, when you need to populating it.
set tableView.delegate (in code or in Interface Builder), reference here
implement:
- (NSView*) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
{
Item* itemView = [tableView makeViewWithIdentifier:#"rowItem" owner:self];
/*Here you populate your cell view*/
id entryObject = [self.entries objectAtIndex:row];
[itemView setEntry:entryObject];
return itemView;
}
and then invoke [tableView reloadData];
maybe for you better to use this method
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
Just see the NSTableViewDataSource and NSTableViewDelegate

Build an array with input from tableviewcell UITextfield

I have a tableview with dynamical amount of cells, based on an array who is build up by a JSON.
tableData = [NSJSONSerialization JSONObjectWithData:dataWebService options:kNilOptions error:&error];
This little line gets all the data I need to build my tableview.
In the tableview I have a textfield in every cell with predefined text from my JSON. The user can change this value before he proceed to the next segue (storyboard).
The question is how I can store this usertyped values in an array an replace the excisting values in my tableData array before I send this arraobject to the next segue.
And keep in mind that the user input must not change when the user scroll in the tableview.
Maybe this is basic, but I cannot figure a nice way to do this.
This is an example of the JSON.
(
{
defaultval = 8;
dtype = long;
id = "#franr";
label = "Faktura nr.";
},
{
defaultval = 8;
dtype = long;
id = "#tilnr";
label = "Til faktura";
}
The tableview look like this:
(I cannot post images)
Fra faktura 8
Til faktura 8
The user can change the values (number 8) to any numbers. So I need to replace the numbers of defaultVal in my JSON array.
Thanks.
Since its a UITEXTFIELD, you can use OnTextChange Event
[textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
or
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
, or lose focus
- (BOOL)textFieldShouldReturn:(UITextField *)textField
please keep in mind you need to set the delegate of the textfield to self & add the respective protocals.
In order to keep the data after scrolling, you should modify the data source (the array) after each input from the user. You can do dis in the UITextFieldDelegate's textFieldDidEndEditing: method (of course, you have to be the delegate for each text field: textField.delegate = self;).
You should also implement a UIScrollViewDelegate method, for dismissing the keyboard when the table begins to scroll (e.g. scrollViewWillBeginDragging:).
EDIT:
Your array must be containing NSDictionaries, which are not mutable, so I recommend, you create your own class having those attributes (defaultval, dtype ...), so you can edit the data source. I don't think you can modify your array the way it is now.
The code with custom object would be something like:
- (void)textFieldDidEndEditing:(UITextField *)textField {
UITableViewCell *cell = textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
MyObject *currentObject = [tableData objectAtIndex:indexPath.row];
currentObject.defaultVal = textField.text.intValue;
}

Accessing UI Input elements as I would in HTML

I am dumping multiple inputs into a view. They consist of UITextFields UIPickerViews and UIDatePickers.
Each of them have an ID and a Key that need to be saved when the input value is saved. So when the 'Save' button is clicked, I need to loop through and store something like:
{
ID: 'inputid',
Key: 'yearly',
Value: (UITextField value)
}
In HTML, I would just add these values to the input (<input type="text" id="inputid" name="yearly" />) and then loop through each one using $(input).attr('id') etc.
In Objective-C, the only way I can think to do this is to keep a hashtable of this information when I draw the inputs, and then store some kind of identifier against the 'tag' field of the UITextField, then read that by getting all of the inputs from a view and comparing them to the hashtable.
Is this the right way to go about it?? Am I missing something simple here? How would you go about it?
EDIT
To better frame the situation, the number of UITextFields on the page is being pulled from an XML file, therefore I don't know how many UITextFields there will be (so can't assign them to the controller necessarily)
I need something along the lines of:
foreach(var question in ArrayOfQuestions) {
UITextField *textField = [[UITextField alloc] initWithFrame:];
textField.attributes["id"] = question.Id;
textField.attributes["key"] = question.Key;
}
and in the save method
foreach(var textField in UIView) {
textField = (UITextField)textField;
NSString *id = textField.attributes["id"];
NSString *key = textField.attributes["key"];
}
This maybe something I could find in google but can't think of the right search terms and keep coming up empty handed. On the same level, if you can better describe my request please update the title of my question
I think you are actually at the best solution, in regards to the hash table (NSDictionary) of attribute data. It is really a bad design decision to have too much semantic data in the view object itself, as it has nothing to do with the view.
What you need to do concretely in code is the following:
To set up your views & attribute data:
UIView *containerView; // The view that contains your UITextViews.
NSMutableDictionary *attributes; // A dictionary mapping tags to questions.
NSMutableArray *arrayOfQuestions; // The questions that you've parsed from a file or whatever.
// ...
// Each "question" would be of the form #{ #"id" : ____, #"key" : ____ }
for (NSDictionary *question in arrayOfQuestions) {
UITextField *textField = [[[UITextField alloc] initWithFrame:aFrame] autorelease];
[containerView addSubview:textField];
textField.tag = getATag(); // However you want to tag them.
// Fancy new objective-C container/object-literal syntax :)
attributes[#(textField.tag)] = question;
}
Then for your "save" method:
for (UIView *childView in containerView.subviews) {
if ([childView isKindOfClass:[UITextView class]]) {
// We know the class and can thus safely typecast the UIView.
UITextField *textField = (UITextField *)childView;
NSDictionary *aQuestion = attributes[#(textView.tag)];
// Now you can access the id and key properties of the question.
// ... Whatever else you want to do.
}
}
The enumerated loop over the subviews is I think the big thing you were looking for here. It is very similar to the way that you would do it in jQuery with selectors.
If you make each of the elements a property of the view controller, you can access them directly from anywhere and get the current value.
So in the method attached to the save button, you can get the current string value of a UITextField like this, for example:
NSString *currentTextFieldString = self.someTextField.text;

Obtain a stringValue from NSTableView

I have a simple NSTableView which I have loaded with data from a NSMutableArray. When I select a row (entry) in the tableView and modify it, I can find the row index, what I cannot get is the edited entry out as a string so I can modify the array. I can find lots of information on selecting rows, etc., but not on how to get the actual modified string. I keep thinking this should be real simple. Help please.
Part of my code:
- (IBAction)toDoEdit:(id)sender // Accept the edited data
{
NSString *toDoItem = [[toDoTableCell:toDoTableView dataCellFoTableColumn:0 row:rowToBeEdited] stringValue];
// I get the error "dataCellForTableColumn' method cannot be found.
[toDoArray replaceObjectAtIndex:rowToBeDeleted withObject:toDoItem];
[toDoTableView reloadData];
[toDoTableView deselectRow:rowToBeDeleted];
}
~~~~~~~~~~~
// This method should return the cell value of the selected row
- toDoTableCell:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
return toDoTableCell; // No errors here but toDoTableCell is nil.
}
The 'Add' data to tableView works, 'Delete' data from tableView works, I just cannot get the edited data out of tableView so I can reload the data with the corrections.
What you are looking for is an NSTableView Delegate method:
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
This will return the NSCell that is in the row and column that you specify. From the NSCell you should be able to extract the value that you need. Depending on how you are using your NSCell you would either call [cell stringValue] or [cell objectValue].
Try this:
– tableView:setObjectValue:forTableColumn:row:
in - NSTableViewDataSource Protocol Reference
--- Edited based on comment ---
Above method is called whenever user tries to edit a table row, it also provides user with changed value as parameter. If you are trying to edit the row in table itself then it should serve your purpose. You can simply check the objectValue obtained as parameter and verify if it is correct or not. In case it is incorrect you can modify the obtained value and set it in todoArray.
Briefly:
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
// below case is an example, you can add your own
if([anObject isEqualToString:#"Incorrect"])
{
anObject = #"Correct";
}
// Considering todoArray is array of dictionary items containing keys as table-column identifiers
NSMutableDictionary *originalData = [todoArray objectAtIndex:rowIndex];
[originalData setValue:anObject forKey:[aTableColumn identifier]];
[toDoTableView reloadData];
}
To get the value being edited you can simply use this code in above method, before setting the new value:
NSString *editedValue = [[todoArray objectAtIndex:rowIndex] valueForKey:[aTableColumn identifier]];
Hope this helps :)
It is simple. Read up on Cocoa Bindings and NSArrayController.
Take a look at the NSTableView methods selectedColumn, selectedColumnIndexes, selectedRow and selectedRowIndexes. I guess they should provide you with the needed information.
Now you can query the model, i.e. the array, for the data you need.

Getting checkboxes to reflect their state in an NSTableColumn

I'm making this program that has an NSTableView with four columns, two of which are make of checkboxes. I'm only trying to get one working right now and I've gotten stuck.
First, here's my relevant code:
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
NSString *filePathThree = [[NSBundle mainBundle] pathForResource:#"mydictionary" ofType:#"plist"];
NSData *myDataThree = [[NSData alloc]initWithContentsOfFile:filePathThree];
self.flozzCodeAndName = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:myDataThree
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:NULL
errorDescription:NULL];
return [[flozzCodeAndName objectForKey:#"name"] count];
}
- (void)tableView:(NSTableView *)tableView
setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
{
NSButtonCell *cell;
cell = [[NSButtonCell alloc] init];
[cell setButtonType:NSSwitchButton];
[cell setTitle:#""];
[cell setTag:rowIndex];
NSLog(#"%d", [cell tag]);
[cell setCellAttribute:NSCellEditable to:3];
[cell setImagePosition:NSImageOnly];
[cell setState:NSOnState];
NSLog(#"%d", [cell state]);
[havzColumn setDataCell:cell];
[myTableVeew reloadData];
[cell release];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSString *filePathThree = [[NSBundle mainBundle] pathForResource:#"mydictionary" ofType:#"plist"];
NSData *myDataThree = [[NSData alloc]initWithContentsOfFile:filePathThree];
self.flozzCodeAndName = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:myDataThree
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:NULL
errorDescription:NULL];
NSArray *myArray = [flozzCodeAndName objectForKey:[aTableColumn identifier]];
NSString *myStringValue = [myArray objectAtIndex:rowIndex];
return myStringValue;
}
As you can see, I'm using the data source method for this table rather than bindings. The book I read for Cocoa made some checkboxes with tags, but I think they were in an array, so that might not be the best thing to do.
Anyway, when I run this, the debugger will show me the tag (which equals the row) along with the state of the button (1 for all of them because of NSOnState). My problem is that I cannot get the boxes to check and uncheck depending on their state. I read this question: Checkbox on table column won't register click
And then the NSTableView datasource reference. According to Mr. Nozzi in the linked question, it seems to me that an array containing the states for the boxes is needed, so I tried that, setting [cell state] to an NSNumber to get it into an NSMutableArray. I FUBAR'd that and don't think that was right. There are 454 rows in this table (tags go to 453 because of arrays starting at 0), for all four columns.
I also wonder if I should put the cell definition stuff that is in tableview:setObjectValue: into an 'awakeFromNib'. I did put a checkbox button cell in the IB, but I was having problems with it earlier, so I decided to do it programmatically too. During all of these, I did, and still do, have a [myTableVeew reloadData] in the setObjectValue method.
The assistance is appreciated, if any other info is needed, I can get it.
You have two problems: Your data source keeps getting blown away and you're not using the ...objectValue... method properly.
Data Source:
You're blowing away your data source in your -numberOfRowsInTableView: method and replacing it every time the table needs to do a refresh. You'll want to cache (a mutable copy of) your dictionary only when you need to (like at application launch) then only refer to it from the table data source methods. Perhaps you should move it to an instance variable and use proper accessors.
Also, the documentation mentions that, because the data source methods are called very frequently, they should be fast, so from a performance viewpoint alone this is not a good idea. You should only do what it takes to answer the question the delegate method is posing to keep the table responsive with large data sets.
Object Value: You should ONLY be returning the object value from this method (usually an NSNumber object containing the state the checkbox is meant to toggle.
You should set your table column's -dataCell when the view is loaded or at application launch. Even easier: drag a check box cell into the table column in Interface Builder to set that as the data cell without code.
Additional Observation: If you plan to persist the changes to this information in any way, note that you must never rely on the application bundle being writable and should never attempt to overwrite resource files like the one you're loading from your bundle. You'll need to save the information elsewhere, using your bundle copy as a template copy only.