在NSTableCellView中定位文本

时间:2012-11-29 17:00:05

标签: objective-c cocoa

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

我做错了什么?如何正确地将这些爆破的行放大?

1 个答案:

答案 0 :(得分:3)

您是否尝试过委托方法:

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