I have a view based tableview with a button which displays a popover:
How would I go about setting the selected to the row that holds the pressed button.
I'm using core data, with an array controller. I plan to have the tableview in the popover show related objects.
Just add a tag equal to row to every cell button, button.tag = indexPath.row;
Then in your button's selector you just call
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:button.tag inSection:0]];
Related
I have two View Controllers. First one have sendbutton that segue to second view controller. Action Segue is Show. Each time click the button, new window pops up again. I don't want create new window again, and I just want to only one second View Controller.
And my FirstViewController.m :
- (void)prepareForSegue:(NSStoryboardSegue*)segue sender:(id)sender{
SecondViewController *destinationScene = [segue destinationController];
destinationScene.receivedString = _nextOne.stringValue;
}
SecondViewController.m :
- (void)viewWillAppear{
if(_myMutableArray == nil) _myMutableArray = [NSMutableArray array];
[_myMutableArray addObject:_receivedString];
}
What I want to is send a string in the first View Controller's TextField repeatedly to next View Controller. And keep using only one second window that created at first time.
See my Storyboard
You can do this in IB without any code. Update the Presentation attribute for the view controller in the destination view from Multiple to Single:
https://stackoverflow.com/a/36104388/287963
Don't set up the segue to happen automatically. Create an IBAction for the button, test whether your second view controller is already on the screen, and either segue or not depending on whether it already exists.
I'm trying to create a custom NSView to display in a column in a view-based NSTableView. The view contains 2 subviews, an NSTextField and an NSButton. I want the button to stay the width set by the constraints, and the textfield to resize when the NSView is resized. Below is a small animation showing the NSView and its subviews, and the constraints I created.
As you can see, resizing the NSView works as expected.
Now, when displaying this custom NSView in an NSTableView, it looks as if the button just disappears, and resizing the column makes the textfield resize with it (the 'Category' column).
The coded used to create the NSView in tableView:viewForTableColumn:row:
let identifier = tableColumn!.identifier
if identifier == "Category" {
var view = tableView.makeViewWithIdentifier(identifier, owner: self) as? TableCategoryView
if view == nil {
view = TableCategoryView(frame: tableView.frame)
view!.identifier = identifier
}
return view
}
The strange thing is, when there are no constraints on the 2 views, the button and textfield are both happily displayed inside the column, but they then of course don't resize with the table column width.
What am I doing wrong?
EDIT: It looks like something else is wrong. The NSView itself isn't resizing at all with the table column.
I think TableCategoryView is your new class and it is subclassed from NSView and shall replace the NSTableCellView you get when creating (in IB) a view based NSTableView. If you really want to create your own TableCellView it should be a direct subclass of NSTableCellView not NSView.
But in your case (add a button to the TableCellView) you do not need to create a new class. The existing TableCellView object already has a TextField (a property) with the name textField. Then simply drag (means: add) a button into the existing TableCellView (resize it and set the constraints) and drag a link from the button to a corresponding method in the delegate of the TableView. In the "corresponding method" you can ask for the clicked row and column and identify the click button. I did so for a TableView and for me it works well.
I've got a view-based NSTableView, using Cocoa Bindings to change the values of some labels and images in the cell. It all works great. However, I want to add a button to the cell. I've got the button working, but its action method only has the button as sender, which means I have no idea of the content of the cell that the button is in. Somehow I need to store some extra data on the button - at the very least the row index that the button is in. I subclassed NSButton and used my subclass in the cell, but Interface Builder doesn't know about the extra property so I can't bind to it. If I wanted to bind it in code, I don't know the name of the object or keypath that would be passed to it.
How can I get this to work?
You can use rowForView in your action method to get the row value
- (IBAction)doSomething:(id)sender
{
NSInteger row = [_myTableView rowForView:sender];
}
You can use the Identity field in Interface Builder to associate a table cell view from the nib with an instance in your code:
Additionally you have to implement - tableView:viewForTableColumn:row: in your table view's delegate. (Don't forget to connect the delegate in IB)
- (NSView*)tableView:(NSTableView*)tableView viewForTableColumn:
(NSTableColumn*)tableColumn row:(NSInteger)row
{
SSWButtonTableCellView *result = [tableView makeViewWithIdentifier:#"ButtonView" owner:self];
result.button.title = [self.names objectAtIndex:row][#"name"];
result.representedObject = [self.names objectAtIndex:row];
return result;
}
I added representedObject property in my NSTableCellView subclass, which I set in the above table view delegate method.
Your custom table cell view can later use that object in it's action. e.g.:
- (IBAction)doSomething:(id)sender
{
NSLog(#"Represented Object:%#", self.representedObject);
}
In my app's navigation controller is an UITableViewContoller then a ViewController. The user selects an item in the UITableViewContollrer, this calls the ViewController. If the user hits the back button I need to visually show which item was selected before. Doing this with a UIImage, basically want to hide or show it. Problem is I cannot find a method that will parse each cell in the UITableView and determine if the the cell was selected before when the UITable is called again.
Some background on the UITableView, it is based on an entity in core data. The selected item is stored in a different entity in core data.
Firstly declare NSIndexPath *selectedIndexPath and creates its property. Now in tableView Delegate:
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
{
selectedIndexPath = indexPath;
}
You have selected IndexPath of cell in tableView. When hits back button of navigator bar, ViewControllers viewWillApper will be called
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(selectedIndexPath)
{
//if you have custom cell then you will get custom cell which was selected just use custom cell object in place of UITableViewCell
UITableViewCell *cell = (UITableViewCell*)[YourtableView cellForRowAtIndexPath:selectedIndexPath];
//You have cell reference which was selected when one view controller was pushed in navigation controller.
// you can change image or label text which are present in your cell
// cell.ImageView or cell.labelText anything
}
}
Hope it is useful.
I have a view-based NSTableView that is populated through bindings. My textFields & imageViews are bound to the NSTableCellView's objectValue's properties.
If I want to have an edit/info button in my NSTableCellView:
Who should be the target of the button's action?
How would the target get the objectValue that is associated with the cell that the button is in?
I'd ultimately like to show a popover/sheet based on the objectValue.
I found an additional answer: The Answer above seems to assume you're using bindings on your table view. Since I'm kind of a noob I found a way to get the button inside the table view cell.
- (IBAction)getCellButton:(id)sender {
int row = [xmlTable rowForView:sender];
}
This way when you click on the button inside the row, you don't have to have the row selected. It will return the int value of the row to match up with a datasource in an array without bindings.
Your controller class can be the target. To get the object value:
- (IBAction)showPopover:(id)sender {
NSButton *button = (NSButton *)sender;
id representedObject = [(NSTableCellView *)[button superview] objectValue];
}
Or, use a subclass of NSTableCellView, make the cell view the target of the button's action, and call [self objectValue] to get the object.