xcode picker output into array - objective-c

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.

Related

Binding image to NSPopupbutton from NSArrayController

This question is an extension to link
(The question in the link mainly targets, binding NSPopupbutton to a NSArrayController)
I have a Person class having properties NSString *name and NSImage *avatar
I have to show all the names of persons in Popup button as seen in the below image.
But now, as requirement has changed, I need to show avatar of person also.
How do I use Cocoa bindings to bind person's avatar to NSPopup button so that it looks like the one in above image for michael(last menu option)
Note: Michael has been temporarily added for demonstration using following code:
person.title = #"Michael";
person.image = [NSImage imageNamed:#"avatar.png"];
[_popupButton.menu addItem:person];
There are two ways you can achieve that:-
First take the cell-based tableview inside that two column one for image cell and second for text cell. Populate the table through bindings and then add your tableview inside your popup button in the below way:-
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:#""];
NSMenuItem *Item = [[NSMenuItem alloc] initWithTitle:#"NameList" action:NULL keyEquivalent:#""];
[Item setView:self.tableVw];
[theMenu addItem:Item];
[self.popUptn setMenu:theMenu];
Second follow the below simple steps:-
Assuming your array contains the name elements.
1) Select your arrayContoller bind to FileOwner's and ModelKeyPath->array.
2) Select PopUpButton inside binding Inspector->Selected Object bind to FileOwner's and ModelKeyPath->yourString. This will select the required name accordingly.
3) Select PopUpButton inside binding Inspector->Content Valuesand ModelKeyPath->array
4) Now for setting the image inside popupButton refer below code:-
NSMenuItem *menuItm=[self.popUpBtn itemWithTitle:#"Michael"];
[menuItm setImage:[NSImage imageNamed:#"dot.gif"]];
Edit:-
1) In the first method your text and image both are populating through binding just you need to add that tableview inside your popupbutton.
2) In the second method your popupbutton will display the image for last name but programmatically. And also, if required to display the images for all names then use for loop to set images inside menu item.

how to create uitableview with different custom cells?

I've got a UIImageView to place a picture there and UITableView to place comments there. The part of the view with UIImageView should be scrolled with comment cells, that's why separate UIImageView and UITableView can't be used - the image won't be scrolled. If UIScrollView is used, than I receive scroll in scroll. Can cellForRowAtIndexPath method be used to create different cells? E.g.:
if (indexPath.row == 0) {
CellWithImage * cell = (CellWithImage *)[tableView dequeueReusableCellWithIdentifier:#"CellWithImage"];
//some code here
return cell;
}
else {
CellWithComment * cell = (CellWithComment *)[tableView dequeueReusableCellWithIdentifier:#"CellWithComment"];
//some code here
return cell;
}
For your case, its better to make the imageview part of the header view for the section
Yes, you can use different identifiers to load different cell types and use them together inside the same table. In fact, your case is somewhat simple -- you're only using a different cell for the first row. You could easily choose the cell type based on the type of data that will be displayed in the row, position of data within a hierarchy, etc.

UITableView setContentOffset but don't scroll tableView?

I am using setContentOffset on a UITableView because I want to initially hide a search field that is my tableHeaderView.
[self.tableView setContentOffset:CGPointMake(0, 56)]; // No scroll please!
Each time I push a new viewController I want to hide the search bar with contentOffset. But when I pop a viewController that offset is no longer in effect for some reason and shows the search bar. Why is this?
you can try and implement it on the following
- (void)viewWillAppear:(BOOL)animated {
[self.tableView setContentOffset:CGPointMake(0, 56)];
}
that will put the table in the correct position before it is displayed on the screen, I am assuming you mean no animation while setting the position.
I am guessing that you want to stop the user being able to scroll to the very top of the screen. If so you can implement the following UITableView delegate (on iOS5 and above):
scrollViewWillEndDragging:withVelocity:targetContentOffset:
which allows you to modify the final target for a change in the contentOffset. In the implementation you do:
- (void)scrollViewWillEndDragging:(UIScrollView *)theScrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if(targetContentOffset->y < 56) {
targetContentOffset->y=56;
}
}
If you are trying to preserve the value of something during an action that loses it, the natural solution is to hold onto it yourself ("Hold/Restore"):
"Hold": get content offset to a field or local variable. Apple doc
.. do whatever you want.
"Restore": set content offset to the value you got above.
(Sorry, I don't write Objective C code, so can't provide the exact code. An edit to add the code, would be welcome.)
In a different situation, it might be necessary to hold the row you were at, and then scroll back to that row:
(Adapted from: https://stackoverflow.com/a/34270078/199364)
(Swift)
1. Hold current row.
let holdIndexPath = tableView.indexPathForSelectedRow()
.. do whatever (perhaps ending with "reloadData").
Restore held row:
// The next line is to make sure the row object exists.
tableView.reloadRowsAtIndexPaths([holdIndexPath], withRowAnimation: .None)
tableView.scrollToRowAtIndexPath(holdIndexPath, atScrollPosition: atScrollPosition, animated: true)

How to get elements inside a selected row in a NSTableView?

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];
}

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?