基于视图的NSTableView具有动态高度的行 [英] View-based NSTableView with rows that have dynamic heights

查看:226
本文介绍了基于视图的NSTableView具有动态高度的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于视图的 NSTableView 的应用程序。在这个表视图中,我有行的单元格的内容由一个多行 NSTextField 启用了字包装。根据 NSTextField 的文本内容,显示单元格所需的行大小会有所不同。

I have an application with a view-based NSTableView in it. Inside this table view, I have rows that have cells that have content consisting of a multi-row NSTextField with word-wrap enabled. Depending on the textual content of the NSTextField, the size of the rows needed to display the cell will vary.

我知道我可以实现 NSTableViewDelegate 方法 - tableView:heightOfRow: code>返回高度,但是高度将根据 NSTextField 上使用的单词包装确定。 NSTextField 的单词wrap也类似地基于 NSTextField 的宽度...由 NSTableView

I know that I can implement the NSTableViewDelegate method -tableView:heightOfRow: to return the height, but the height will be determined based on the word wrapping used on the NSTextField. The word wrapping of the NSTextField is similarly based on how wide the NSTextField is… which is determined by the width of the NSTableView.

Soooo ...我想我的问题是...什么是好的设计模式?似乎一切我试图卷起来是一个复杂的混乱。因为TableView需要知道单元格的高度来布置它们...和 NSTextField 需要知道它的布局来确定单词wrap ...和单元格需要知识

Soooo… I guess my question is… what is a good design pattern for this? It seems like everything I try winds up being a convoluted mess. Since the TableView requires knowledge of the height of the cells to lay them out... and the NSTextField needs knowledge of it's layout to determine the word wrap… and the cell needs knowledge of the word wrap to determine it's height… it's a circular mess… and it's driving me insane.

建议?

如果重要,最终结果也会有可编辑的 NSTextFields ,它将调整大小以适应其中的文本。我已经有这个工作在视图级别,但是tableview还没有调整单元格的高度。一旦我得到了高度问题,我会使用 - noteHeightOfRowsWithIndexesChanged 方法通知表视图的高度改变...但它仍然然后去问问代表。

If it matters, the end result will also have editable NSTextFields that will resize to adjust to the text within them. I already have this working on the view level, but the tableview does not yet adjust the heights of the cells. I figure once I get the height issue worked out, I'll use the -noteHeightOfRowsWithIndexesChanged method to inform the table view the height changed… but it's still then going to ask the delegate for the height… hence, my quandry.

提前感谢!

推荐答案

这是鸡和鸡蛋的问题。表需要知道行高,因为它决定了给定视图的位置。但是你想要一个视图已经在周围,所以你可以使用它来计算出行高。

This is a chicken and the egg problem. The table needs to know the row height because that determines where a given view will lie. But you want a view to already be around so you can use it to figure out the row height. So, which comes first?

答案是保留一个额外的 NSTableCellView 你的单元格视图)只是为了测量视图的高度。在 tableView:heightOfRow:委托方法中,访问您的模型为'row'并设置 objectValue NSTableCellView 。然后将视图的宽度设置为表格的宽度,并且(但是您想要这样做)可以计算出该视图所需的高度。返回该值。

The answer is to keep an extra NSTableCellView (or whatever view you are using as your "cell view") around just for measuring the height of the view. In the tableView:heightOfRow: delegate method, access your model for 'row' and set the objectValue on NSTableCellView. Then set the view's width to be your table's width, and (however you want to do it) figure out the required height for that view. Return that value.

不要在委托方法中调用 noteHeightOfRowsWithIndexesChanged: tableView:heightOfRow: / code>或 viewForTableColumn:row:

Don't call noteHeightOfRowsWithIndexesChanged: from in the delegate method tableView:heightOfRow: or viewForTableColumn:row: ! That is bad, and will cause mega-trouble.

要动态更新高度,你应该做的是响应文本更改(通过目标/操作)并重新计算该视图的计算高度。现在,不要动态改变 NSTableCellView 的高度(或者你用作单元视图的任何视图)。表格必须控制该视图的框架,如果您尝试设置它,您将与tableview对抗。相反,在您计算高度的文本字段的目标/操作中,调用 noteHeightOfRowsWithIndexesChanged:,这将让表调整该行的大小。假设你在子视图上设置了自动调整掩码设置(即: NSTableCellView 的子视图),事情应该调整得很好!

To dynamically update the height, then what you should do is respond to the text changing (via the target/action) and recalculate your computed height of that view. Now, don't dynamically change the NSTableCellView's height (or whatever view you are using as your "cell view"). The table must control that view's frame, and you will be fighting the tableview if you try to set it. Instead, in your target/action for the text field where you computed the height, call noteHeightOfRowsWithIndexesChanged:, which will let the table resize that individual row. Assuming you have your autoresizing mask setup right on subviews (i.e.: subviews of the NSTableCellView), things should resize fine! If not, first work on the resizing mask of the subviews to get things right with variable row heights.

不要忘记 noteHeightOfRowsWithIndexesChanged:

Don't forget that noteHeightOfRowsWithIndexesChanged: animates by default. To make it not animate:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0];
[tableView noteHeightOfRowsWithIndexesChanged:indexSet];
[NSAnimationContext endGrouping];



PS:我回答更多的问题发布在苹果开发论坛比堆栈溢出。

PS: I respond more to questions posted on the Apple Dev Forums than stack overflow.

PSS:我写了基于视图的NSTableView

PSS: I wrote the view based NSTableView

这篇关于基于视图的NSTableView具有动态高度的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆