Get button's row in view based table

这一生的挚爱 提交于 2019-11-30 08:18:31

问题


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.


回答1:


-[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.




回答2:


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.




回答3:


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



回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/10202813/get-buttons-row-in-view-based-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!