Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring the custom view cell above table rows in NSTableView?

When I use a custom view as the cell of a view-based NSTableView, the custom view is somewhat below the table row. When I click on it, instead of affecting the elements (e.g. text field) custom view, the table row was selected (and highlighted). I have to reclick to select the text field.

- (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSLog(@"We are creating views!");
    NSTableCellView *newView;
    newView = [tableView makeViewWithIdentifier:@"PostCell" owner:self];


    NSTextField *newTextField = [[NSTextField alloc] init];
    [newView addSubview:newTextField];

    return newView;
}

highlights the front

When I disable the row selection according to NSTableView - Disable Row Selection, there was no selection.

- (BOOL)selectionShouldChangeInTableView:(NSTableView *)tableView {
    return NO;
}

But I still cannot select directly the text field. What's worse, I cannot even select it using the mouse. Only tab on the keyboard works.

Unable to select

There seem to be something above it. But is it the "table column" shown in interface builder? Or something else?

How can I fix this?

like image 388
Colliot Avatar asked Oct 26 '15 14:10

Colliot


2 Answers

Use a custom subclass of NSTableView and override -validateProposedFirstResponder:forEvent: to return YES.

See this blog entry from the Apple engineer who wrote the view-based table view code.

like image 193
Ken Thomases Avatar answered Nov 06 '22 18:11

Ken Thomases


Make sure following code is present.

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex {
return YES; 
}

You may try logging the subviews Or you can check superviews of view. This will help to understand view hierarchy.

Also on side note if one of the view's userInteraction is disable then it's subview's won't be able to receive the events. Please verify that all the views and it's subviews userInteraction is enable.

I hope this helps.

like image 41
Sourabh P Shroff Avatar answered Nov 06 '22 17:11

Sourabh P Shroff