10

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?

Community
  • 1
  • 1
Colliot
  • 1,522
  • 3
  • 16
  • 29
  • how do you initialize the UITableViewCell? `cell.contentView = customView` ? if yes you should consider adding the view instead `[cell.contentView addSubview:customView]` – blld Oct 29 '15 at 07:51
  • @AurélienBouilland, drag a custom view in the interface builder right under the table column (as the child of it), set the class to my view class, and `NSTableCellView *newView; newView = [tableView makeViewWithIdentifier:@"PostCell" owner:self];`; – Colliot Oct 29 '15 at 16:04
  • @AurélienBouilland, for other details, refer to [Custom view created with Interface Builder does not render when called in other views](http://stackoverflow.com/questions/33329280/custom-view-created-with-interface-builder-does-not-render-when-called-in-other). – Colliot Oct 29 '15 at 16:05
  • Would you mind put some code or screenshot that you struck ? – Sruit A.Suk Nov 04 '15 at 20:50
  • @SruitA.Suk, please look at my updates. – Colliot Nov 19 '15 at 20:51

2 Answers2

4

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1

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.