How do you get the row for a button in a view based table when you click the button? The row is not selected when the button is clicked, but I found that if you log sender.superview.superview in the button's action method, I get: NSTableRowView: 0x1001b3a90 - row: 2. So, the row is there in the log, but I don't know how to get at it programmatically. I have the button's action method in a subclass of NSTableCellView.
Get button's row in view based table
Asked Answered
-[NSTableView rowForView:]
says this in its documentation:
This is typically needed in the action method for an NSButton (or NSControl) to find out what row (and column) the action should be performed on.
Thanks, this is what I needed. I can get the table view in the button's action method from sender.superview.superview.superview and the view from sender.superview –
Yseulte
The action should be sent to a controller and that controller should know the table view. It should have an outlet. So, it shouldn't need to follow the superview chain. And, you can just pass
sender
as the argument to -rowForView:
, because it looks up the row containing, directly or indirectly, the view. –
Wsan I was doing some testing just to see how I could manipulate various UI elements in a view based table, so initially, I had the action method in the app delegate (for simplicity) and that was never called. Then I put it in the custom table cell view because I read somewhere that that's where it should be. As you said in your comment, it really seems that it should be in a controller object, but is there some restriction on where it can go to make it work? Why didn't it work in the app delegate? I'm not sure what the responder chain looks like for a view based table. –
Yseulte
Hello Ken, sorry I'm a little late. But I try to find a similar solution but can't figure out what to do myself. How do I need t use that code snippet
[NSTableView roeForView:]
in my ButtonCell
action method? Does it work for osx (not iOS)? Thanks! –
Impurity What object is the target of your button's action method? It should probably be a controller and that controller should have an outlet that you connect to the table view. In the action method, you might do
[theTableView rowForView:sender]
, which would get the row which contains the button that sent the action method. –
Wsan In Swift 5.1 -
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let checkBoxCell = tableView.makeView(withIdentifier:NSUserInterfaceItemIdentifier(rawValue: "<ColumnIdentifier>"), owner: self) as! NSButton? {
checkBoxCell.tag = row;
checkBoxCell.target = self;
checkBoxCell.action = #selector(TableViewService.checkBoxAction)
return checkBoxCell
}
return nil
}
@objc func checkBoxAction(button:NSButton){
print(button.tag);
}
Here I'm giving you a simple example. In this I'm adding a UIButton in content view. When I clicked on button I call a Method and there I get Row number and call as I required
//Adding a button
UIButton *btnBuyer=[UIButton buttonWithType:UIButtonTypeCustom];
btnBuyer.frame=CGRectMake(238, 10, 26, 32);
btnBuyer.tag=indexPath.row;
[btnBuyer setImage:[UIImage imageNamed:@"buyIcon.png"] forState:UIControlStateNormal];
[btnBuyer addTarget:self action:@selector(goBuyTab:)forControlEvents:UIControlEventTouchUpInside];
[cellNew.contentView addSubview:btnBuyer];
And When User Clicks on this I got Id from the following method
-(void)goBuyTab:(UIButton *)sender{
NSLog(@"after click buy button function called goBuyTab");
NSLog(@"sender.tag in goBuyTab : %d",sender.tag);
int selectedRow=sender.tag;// This is row number
}
Hope this is what you required.
I share this code for Swift 4.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
........
but.action = #selector(ViewController.buttonAction)
.........
}
@objc func buttonAction(but: NSButton){
let tablerow: NSTableRowView = but.superview?.superview as! NSTableRowView;
let index = table?.row(for: tablerow);
print("\(index));
}
in osx you can use this in your button cell action method in the controller:
- (IBAction)yourMethod:(NSButtonCell *)sender {
NSInteger selected = [yourTableView selectedRow];
NSLog(@"sender sends :%ld", selected);
}
you need to set an outlet from the controller to the NSTableView
.
This doesn't always work because it's perfectly possible to click a button in a row without selecting the underlying row. –
Udall
It also doesn't work because (in a cell-based table view)
sender
is the NSTableView
, not the NSButtonCell
. –
Bren © 2022 - 2024 — McMap. All rights reserved.