我创建了一个非常简单的演示项目,其中包含基于视图的表格视图和以下NSTableViewDelegate实现
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSTableCellView *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];
result.textField.stringValue = [NSString stringWithFormat:@"Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld Row %ld end", row, row, row, row, row, row, row, row, row, row, row, row];
return result;
}
通过将NSTextField单元格中的 设置为自动换行,文本字段会自动获得正确的高度:

一旦我改变NSTextFieldfrom Noneto的行为Editable,文本字段只显示一行:

我不知道为什么——有什么想法吗?谢谢!