2016-04-05 16 views
0

私は簡単な問題に遭遇していますが、まだ最適な解決策を見つけることができません。私は別のxibsからのセルビューをロードしているビューベースのNSTableViewを持っています。私のテーブルビューは動的で、ユーザー入力に基づいて、最終的にテーブルデータソースを調整する行を動的に追加したり削除したりします。それぞれのNSTableCellViewsにはボタンがあり、IBActionクリックハンドラをテーブルビューを保持するNSViewにリンクします。私がする必要があるのは、ロジックを処理できるように、テーブルビューでクリックされたボタンの行番号を取得することです。私はそれを行う方法である。ここtableViewSelectionDidChange:(NSNotification *)notificationNSTableViewCell selectedRow number for IBAction click

- (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選択した回答から、このアプローチをベース。

また、私はこの試みた:

- (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を返します。言及する価値がある場合は、私はFileAuthorをTableViewを保持するビューとして設定しているので、IBActionをリンクすることができ、TableCellViewを別のクラスにサブクラス化しました。しかし、私が見る限り、これは問題の一部だとは思わない。確実にそのセルのボタンクリックに基づいてselectedRowの番号を教えてくれる簡単な解決法はありますか?私が上で試した両方のアプローチはそれぞれ-1とnullを返します。

+1

だからどこで動作しないのですか?あなたは何が効いているのかは分かりませんが、何がうまくいかないかは言いません – rocky

+0

問題の内容をさらに明確にするために投稿を更新しました。 – zic10

+0

'rowForView'が動作するはずです。 'self.tblView'は正しいのですか?' sender'は 'self.tblView'のサブビューですか? NSViewは 'self.tblView'のデリゲートですか?ビューベースのテーブルビュー内のオブジェクトは、テーブルビューのデリゲートにのみ接続できます。 – Willeke

答えて

-1

私はNSButtonのタグプロパティに列を設定します:

- (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); 
} 
+0

誰かがdownvote幸せに行って、理由を言わなかったように見えます。 – rocky

-1

はこの

- (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はTAGですが、objcはTAGのより良いオプションです。

-1

UIButtonのサブクラスを作成し、ボタンのNSIndexPathのプロパティを追加します。 cellForRowAtIndexPathメソッドでこのボタンを使用します。セルのインデックスパスをボタンのインデックスパスのインデックスパスに割り当てます。

タップで、送信者からインデックスパスを取得します。あなたのケースでは、そのボタンのインデックスパス。