gpt4 book ai didi

objective-c - 在 NSTableCellView 中定位文本

转载 作者:行者123 更新时间:2023-12-03 16:32:29 26 4
gpt4 key购买 nike

我在 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!

屏幕上的实际结果:

enter image description here

但是,如果我在创建表时指定 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!

结果表如下所示:

enter image description here

我做错了什么?我怎样才能让这些被破坏的行调整到正确的大小?

最佳答案

您是否尝试过委托(delegate)方法:

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;

关于objective-c - 在 NSTableCellView 中定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com