什么替换UITableViewAutomaticDimension应该使用在NSTableView中获得相同的效果? [英] What replacement for UITableViewAutomaticDimension should I use to get the same effect in NSTableView?

查看:738
本文介绍了什么替换UITableViewAutomaticDimension应该使用在NSTableView中获得相同的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS项目中,我会这样做:

  tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 30

并且正确调整单元格大小。



我应该怎么做才能在macOS项目中重现相同的行为?






Ok, func tableView _ tableView:NSTableView,heightOfRow row:Int) - > CGFloat



我应该配置我的SubView(单元格视图)(实际是NSView)两次...



两种方法 - heightOfRow viewFor tableColumn



听说过重复使用细胞,但是可以在没有nibs的情况下做?



无论如何,如果这样做,我得不到预期的效果。



委托/资料来源:

  let items = [second label,
第二个标签与alotoftext alotof ...,//长行
第二个标签]

func numberOfRows(在tableView:NSTableView) - > int {
return 3
}

func tableView(_ tableView:NSTableView,heightOfRow row:Int) - > CGFloat {
let cell = SubView(text:items [row])
cell.layoutSubtreeIfNeeded()
return cell.frame.size.height
}

func tableView(_ tableView:NSTableView,viewFor tableColumn:NSTableColumn ?, row:Int) - > NSView? {
return SubView(text:items [row])
}

SubView :

  init(text:String){
super.init(frame:CGRect.zero)

let text1 = NSTextField(labelWithString:first label)
let text2 = NSTextField(labelWithString:text)

text2.lineBreakMode = .byWordWrapping
text2.setContentCompressionResistancePriority (100,for:.horizo​​ntal)

self.addSubview(text1)
self.addSubview(text2)

text1< - [Top
text2< - [Top(0).to(text1,.bottom),Left(5),Right(5),Bottom(5)]

}

最后两行是



我的错误在哪里?






:我应该怎么做,使单元格垂直增长/减少,以适应其中的所有子视图相应地调整宽度的tableView?



哦,似乎已经实现。 p>

只需添加观察者:

  name:NSNotification.Name.NSViewFrameDidChange, 
object:self.scrollView

  let vr = scrollView.contentView.visibleRect 
let indexes = tableView.rows(in:vr).toRange()!
tableView.noteHeightOfRows(withIndexesChanged:IndexSet(integersIn:indexes))






是的。相同的视图(SubView)工作完美没有tableView。



但是,我仍然有上述的错误大小的cellView的问题。



PS:对不起我的英语,我希望一切清楚。

解决方案

在MacOS它是一个有点更困难,但不是很难做。
这是一个很好的答案corbin dunn,作者 NSTableView 这里



简单说一下:

你需要知道单元格的大小并返回该值在

  func tableView(NSTableView,heightOfRow:Int)

这是 NSTableViewDelegate 的方法。



Update 20.11.2016



您即将到达!您创建的视图没有设置宽度。所以我想,视图本身假设它比tableView窄得多,因此更高。

  func tableView(_ tableView:NSTableView,heightOfRow row:Int) - > CGFloat {
let cell = SubView(text:items [row])
cell.frame.size.width = tableView.frame.size.width
cell.layoutSubtreeIfNeeded()
return cell.frame.size.height
}

我没有测试过这个代码,应该是。


In iOS projects I do something like that:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 30

and have nicely resizing cells.

What should I do to reproduce the same behaviour in macOS project?


Ok, func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat.

Should I configure my SubView (cell view) (that actually is NSView) twice...

In both methods - heightOfRow and viewFor tableColumn?

I heard about reusing of cells, but can it be done without nibs?

Anyway, if I doing so I get not quite the intended result.

Delegate/Datasource:

let items = ["second label",
             "second label with alotoftext alotof...", // long line
             "second label"]

func numberOfRows(in tableView: NSTableView) -> Int {
    return 3
}

func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
    let cell = SubView(text: items[row])
    cell.layoutSubtreeIfNeeded()
    return cell.frame.size.height
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    return SubView(text: items[row])
}

SubView:

init(text: String) {
    super.init(frame: CGRect.zero)

    let text1 = NSTextField(labelWithString: "first label")
    let text2 = NSTextField(labelWithString: text)

    text2.lineBreakMode = .byWordWrapping
    text2.setContentCompressionResistancePriority(100, for: .horizontal)

    self.addSubview(text1)
    self.addSubview(text2)

    text1 <- [Top(5), Left(5), Right(5)]
    text2 <- [Top(0).to(text1, .bottom), Left(5), Right(5), Bottom(5)]

}

Last two lines are EasyPeasy constraints.

So, result is:

Where is my mistake?


And just one more question: what should I do to make cells grow/decrease vertically to fit all subviews inside of them accordingly to resizing width of tableView?

Oh, seem to have realized.

Just added the observer:

name: NSNotification.Name.NSViewFrameDidChange,
object: self.scrollView

And

let vr = scrollView.contentView.visibleRect
let indexes = tableView.rows(in: vr).toRange()!
tableView.noteHeightOfRows(withIndexesChanged: IndexSet(integersIn: indexes))

in selector method.


Yep. The same view (SubView) works perfect without tableView.

But I still have the above-described problem with wrong-sized cellViews.

P.S.: Sorry for my english, I hope everything is clear.

解决方案

On macOS it is a little more difficult, but not really hard to do. There is an excellent answer on this by corbin dunn, the author of NSTableView here.

To put it in a nutshell:
You need to know the size of your cell and return that value in

func tableView(NSTableView, heightOfRow: Int)

which is a method from NSTableViewDelegate.

Update 20.11.2016:

You are almost there! The view you create has no width set. So I guess that the view itself assumes that it is much narrower than the tableView and therefore higher.

func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
    let cell = SubView(text: items[row])
    cell.frame.size.width = tableView.frame.size.width
    cell.layoutSubtreeIfNeeded()
    return cell.frame.size.height
}

I have not tested this code but it should be it.

这篇关于什么替换UITableViewAutomaticDimension应该使用在NSTableView中获得相同的效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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