我遇到了一个简单的问题,但还没有找到最优的解决方案。我有一个基于视图的NSTableView,它从不同的xibs加载它的单元视图。我的表视图是动态的,根据用户输入,我将动态地添加和删除行,最终调整表数据源。我的每个NSTableCellViews都有一个按钮,我将IBAction单击处理程序链接到保存表视图的NSView。我需要做的是获取在表视图中单击的按钮的行号,这样我就可以处理逻辑了。我可以在tableViewSelectionDidChange:(NSNotification *)notification中成功地做到这一点
我是这样做的:
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
NSTableView *tableView = [notification object];
NSInteger selectedRow = [tableView selectedRow];
}对于实际单击行的用户来说,这非常有效。现在,当我移动NSButton IBAction并将其链接到NSView中时,如下所示:
- (IBAction)buttonClickHandler:(NSButton *)sender {
NSInteger selectedRow = [self.tblView rowForView:sender];
NSLog(@"%ld", (long)selectedRow);
}我从this selected answer中选择了这种方法。
我还尝试了这个:
- (IBAction)buttonClickHandler:(NSButton *)sender {
id representedObject = [(NSTableCellView *)[sender superview] objectValue];
NSLog(@"%@", representedObject);
}
//My configuration
- (void)configureView {
[self.view setFrame:[self bounds]];
[self addSubview:self.view];
[self.view setWantsLayer:YES];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
self.tblView.delegate = self;
self.tblView.dataSource = self;
[self.tblView setIntercellSpacing:NSMakeSize(0, 0)];
[self.tblView registerNib: [[NSNib alloc] initWithNibNamed:@"ParentCellXib" bundle:nil] forIdentifier:@"ParentCell"];
[self.tblView registerNib: [[NSNib alloc] initWithNibNamed:@"ChildCellXib" bundle:nil] forIdentifier:@"ChildCell"];
[self.tblView registerNib: [[NSNib alloc] initWithNibNamed:@"HeaderCellXib" bundle:nil] forIdentifier:@"HeaderCell"];
}但是表示的对象返回null。如果值得一提的是,我已经将我的文件的所有者设置为包含tableView的视图,这样我就可以链接IBAction,并且我已经将TableCellView子类化到不同的类。然而,据我所知,我不认为这是问题的一部分。有没有一种简单的解决方案,可以根据单元格中的按钮单击,可靠地给出selectedRow编号?我在上面尝试过的两种方法分别返回-1和null。
发布于 2016-04-05 08:54:58
我会在NSButton的tag属性中设置行:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
SomeTableCellView *cell = [tableView makeViewWithIdentifier:@"cell" owner:self];
if (cell == nil) {
cell = // init some table cell view
cell.identifier = @"cell";
}
cell.button.tag = row;
[cell.button setTarget:self];
[cell.button setAction:@selector(buttonAction:)];
}
- (IBAction)buttonAction:(id)sender {
NSLog(@"row: %d", sender.tag);
}发布于 2016-04-05 13:24:34
尝尝这个
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
yourCustomeCell *aCell;
NSString *aStrIdentifier = @"yourIdentiFier";
aCell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:aStrIdentifier];
//you have to set your indexpath
objc_setAssociatedObject(aCell.btnUpload_or_Add, @"objBtn", indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[aCell.YourButton addTarget:self action:@selector(yourButtonActiontapped:) forControlEvents:UIControlEventTouchUpInside];
return aCell;
}
-(IBAction)yourButtonActiontapped:(UIButton *)sender{
NSIndexPath *aIndPath = objc_getAssociatedObject(sender, @"objBtn");
NSLog(@"row:%@",aIndPath.row);
}还必须导入
#import <objc/runtime.h>
在IBAction中获取行的另一种方法是标记,但objc是更好的选择。
发布于 2016-04-05 14:20:09
创建UIButton的子类,并为按钮的NSIndexPath添加一个属性。在cellForRowAtIndexPath方法中使用此按钮。将单元格的索引路径分配给按钮的索引路径。
在Tap上,从其发送方获取索引路径。在您的情况下,该按钮的索引路径。
https://stackoverflow.com/questions/36415114
复制相似问题