tableView:heightOfRow:の理解を考え出そうとして、壁に頭をぶつけてきました。助けが必要だと思います。行内のtextViewに対して動的な行の高さを実行したいのですが、アプローチを理解できないようです。私は見つけることができるすべてを読みました、それは本当に多くはありません。このメソッドを使用して、行のサイズを希望どおりに設定できますが、テーブルのサイズが変更されている場合に限ります。表示されていない行は、表示されてテーブルのサイズが変更されるまで、適切なサイズになりません。
tableView:didAddRowView:forRowメソッドを追加しましたが、同じ基本的な考え方を使用すると、行サイズが1行になります。同じコードであっても、tableView:heightOfRow:と同じようには機能しません。textViewの境界を設定するtableView:didAddRowView:forRowメソッドがどういうわけかスケーリングされていると思います。
これが私の(うまくいけば関連する)コードです:
- (CGFloat)tableView:(NSTableView *)tv heightOfRow:(NSInteger)row {
if (tv == dataTableView) {
NSInteger valueCol = [tv columnWithIdentifier:@"value"];
NSTableCellView *valueView = [tv viewAtColumn:valueCol row:row makeIfNecessary:NO];
if (valueView) {
// Working on the interesting column and this row is visible
NSRect bounds = [[valueView textField] bounds];
id value = [[valueView textField] stringValue];
NSFont *fieldFont = [[valueView textField] font];
CGFloat adjustedHeight = [value heightForWidth:bounds.size.width font:fieldFont];
CGFloat rowHeight = [tv rowHeight];
if (adjustedHeight <= rowHeight) adjustedHeight = rowHeight;
return adjustedHeight;
}
}
return [tv rowHeight];
}
- (void)tableView:(NSTableView *)tv didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
if (tv == dataTableView) {
NSInteger valueCol = [tv columnWithIdentifier:@"value"];
NSTableCellView *colView = [rowView viewAtColumn:valueCol];
NSRect textFieldViewBounds = [[colView textField] bounds];
NSTextField *colTextField = [colView textField];
NSFont *colFont = [colTextField font];
id value = [colTextField stringValue];
CGFloat newHeight = [value heightForWidth:textFieldViewBounds.size.width font:colFont];
NSSize colViewSize = colView.bounds.size;
colViewSize.height = newHeight;
textFieldViewBounds.size.height = newHeight;
[colTextField setBounds:textFieldViewBounds];
}
}
更新:新しいコードはより適切に機能していますが、初期ロード時および場合によってはスクロール時にグリッチがあります:
- (CGFloat)tableView:(NSTableView *)tv heightOfRow:(NSInteger)row {
if (tv == dataTableView) {
NSInteger valueCol = [tv columnWithIdentifier:@"value"];
NSTableCellView *valueView = [tv viewAtColumn:valueCol row:row makeIfNecessary:NO];
if (valueView) {
// Working on the interesting column
NSRect bounds = [[valueView textField] bounds];
id value = [[valueView textField] stringValue];
NSFont *fieldFont = [[valueView textField] font];
CGFloat adjustedHeight = [value heightForWidth:bounds.size.width font:fieldFont];
CGFloat rowHeight = [tv rowHeight];
if (adjustedHeight <= rowHeight) adjustedHeight = rowHeight;
return adjustedHeight;
}
}
return [tv rowHeight];
}
- (void) tableView:(NSTableView *)tv didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
if (tv == dataTableView) {
[dataTableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, 1)]];
}
}