【发布时间】:2014-02-07 19:19:29
【问题描述】:
我需要一些关于 NSTableView 和动态行高的帮助
我有什么:
绑定到数组控制器的基于单列视图的 NSTableVIew。每个 NSTableCellView 包含三个子视图:NSImageView、NSTextField(单行)和 NSTextField(多行)。基本上,这是一个聊天界面,所以你会有一个消息、发件人和头像的列表。
我想要达到的目标:
当文本长于行的最小高度时,行会扩展以适应内容。就像 iMessage 一样,气泡会扩大以适应消息。
...这似乎是一件很自然的事情,但是在我在网上找到的所有相关解决方案中,(Ref 1,Ref 2),没有一个对我有用。
参考 2 看起来写得非常棒,但这些都不适用于我的应用程序,因为示例项目使用第三方自动布局代码,并且整个东西都是为 iOS 设计的。参考文献 1 给出了一个非常有前途的解决方案,它是用英语写的。我尝试使用解决方案中描述的“虚拟视图”进行设置,但甚至无法正确更改和测量高度。
这是我的tableView:heightOfRow: 代码,_samplingView 是虚拟视图,它有工作限制,与tableView 中的相同。
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
NSTextField *textField;
NSTextFieldCell *messageCell;
for (NSView *subview in [_samplingView subviews]) {
if ([[subview identifier] isEqualToString:@"message"]) {
textField = (NSTextField*)subview;
messageCell = ((NSTextField*)subview).cell;
}
}
Message *message = [[_messagesArrayController arrangedObjects] objectAtIndex:row];
_samplingView.objectValue = message;
CGFloat width = [[[tableView tableColumns] objectAtIndex:1] width];
[_samplingView setBounds:NSMakeRect(0, 0, width, CGFLOAT_MAX)];
[_samplingView display];
CGFloat optimalHeight = 10 + [messageCell cellSize].height; //messageCell's size stays the same when I change samplingView to try to measure the height
return optimalHeight;
}
结果:所有行高都保持不变,不知何故,当我更改_samplingView 的宽度时,它不会重新调整messageCell 的大小。我认为自动布局会处理这种压缩/扩展,并允许我测量高度。确实,我很困惑。
编辑:供参考,这就是我的观点
+-----------+---------------------------------------------------+
| | NSTextField |
|NSImageView| sender |
| avatar +---------------------------------------------------+
| | |
| | NSTextField (multiline) |
+-----------| message |
| | |
| | (high compression/hugging priority) |
| | (this view should decide the height of row) |
| | |
+-----------+---------------------------------------------------+
【问题讨论】:
-
你找到解决办法了吗?
-
@Jai,我试了一下。
标签: objective-c cocoa interface-builder xcode5 nstableview