我在 Cocoa 中遇到问题,当我定义 tableView 的 rowHeight 时,NSTableCellView 的 textField 中的文本被截断。应该注意的是,所有元素都是以编程方式创建的。我对 Cocoa 有点陌生,我知道下面的很多内容可能低于狗早餐的质量,欢迎提出建议。
我的表是这样创建的:
....
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, returnView.frame.size.width, 100)];
NSTableView * tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, tableContainer.frame.size.width, 100)];
[tableView setRowHeight:30];
NSLog(@"tableView was created with a row height of %f", [tableView rowHeight]);
....
在 viewForTableColumn 方法中,我执行以下操作:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// get an existing cell with the MyView identifier if it exists
NSTableCellView *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];
// There is no existing cell to reuse so we will create a new one
if (result == nil) {
result = [[NSTableCellView alloc] initWithFrame:NSMakeRect(0, 0, tableView.bounds.size.width, [tableView rowHeight])];
// the identifier of the NSTextField instance is set to MyView. This
// allows it to be re-used
result.identifier = @"MyView";
}
// result is now guaranteed to be valid, either as a re-used cell
// or as a new cell, so set the stringValue of the cell to the
// nameArray value at row
NSLog(@"tableView row height is %f", [tableView rowHeight]);
NSLog(@"Cell bounds is {%f,%f} %f x %f", result.bounds.origin.x, result.bounds.origin.y, result.bounds.size.width, result.frame.size.height);
NSTextField *cellTF = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, result.bounds.size.height)];
NSLog(@"TextField size is {%f,%f} %f x %f", cellTF.frame.origin.x, cellTF.frame.origin.y, cellTF.frame.size.width, cellTF.frame.size.height);
[result addSubview:cellTF];
result.textField = cellTF;
[cellTF setBordered:NO];
[cellTF setEditable:NO];
[cellTF setDrawsBackground:NO];
result.textField.stringValue = @"hello";
NSLog(@"cell string value is now %@", [[result textField] stringValue]);
return result;
}
我的日志输出如下(每行都有重复的日志条目):
WHCCConnect[26394:f07] tableView was created with a row height of 30.000000
WHCCConnect[26394:f07] tableView row height is 30.000000
WHCCConnect[26394:f07] Cell bounds is {0.000000,0.000000} 200.000000 x 30.000000
WHCCConnect[26394:f07] TextField size is {0.000000,0.000000} 100.000000 x 30.000000
WHCCConnect[26394:f07] cell string value is now hello row!
屏幕上的实际结果:
但是,如果我在创建表时没有指定 rowHeight,如下所示:
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, returnView.frame.size.width, 100)];
NSTableView * tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, tableContainer.frame.size.width, 100)];
//[tableView setRowHeight:30];
我得到这个作为输出(每行的重复日志条目被编辑):
WHCCConnect[26458:f07] tableView was created with a row height of 17.000000
WHCCConnect[26458:f07] tableView row height is 17.000000
WHCCConnect[26458:f07] Cell bounds is {0.000000,0.000000} 203.000000 x 17.000000
WHCCConnect[26458:f07] TextField size is {0.000000,0.000000} 100.000000 x 17.000000
WHCCConnect[26458:f07] cell string value is now hello row!
结果表如下所示:
我究竟做错了什么?我怎样才能让这些爆破的行正确调整大小?