gpt4 book ai didi

swift - 跟踪 NSCell 的位置变化

转载 作者:行者123 更新时间:2023-11-28 12:29:30 24 4
gpt4 key购买 nike

我有一个 NSTableView,当用户滚动 tableView 时,我想跟踪包含它的 NSCell 的位置。

我找不到任何有用的东西。如果有人能引导我走向正确的方向,那就太好了!

编辑:

感谢 @Ken Thomases@Code Different,我刚刚意识到我正在使用基于 View 的 tableView,使用 tableView(_ tableView:viewFor tableColumn:row:),它返回一个 NSView。

但是,NSView 本质上是一个 NSCell。

let cell = myTableView.make(withIdentifier: "customCell", owner: self) as! MyCustomTableCellView // NSTableCellView

所以我真的希望我最初的问题没有误导。我仍在寻找一种方法如何跟踪单个单元格/ View 的位置

我在 IB 中将 NSScrollView(包含 tableView)的行为设置为 Copy on Scrollenter image description here

但是当我检查 View /单元格 frame 的 x 和 y(在我的 MyCustomTableCellView 子类的 viewWillDraw 中)时,它仍然是 0, 0.

最佳答案

NSScrollView 不使用委托(delegate)。它使用通知中心通知观察者发生了变化。下面的解决方案假设垂直滚动。

override func viewDidLoad() {
super.viewDidLoad()

// Observe the notification that the scroll view sends out whenever it finishes a scroll
let notificationName = NSNotification.Name.NSScrollViewDidLiveScroll
NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(_:)), name: notificationName, object: scrollView)

// Post an intial notification to so the user doesn't have to start scrolling to see the effect
scrollViewDidScroll(Notification(name: notificationName, object: scrollView, userInfo: nil))
}

// Whenever the scroll view finished scrolling, we will start coloring the rows
// based on how much they are visible in the scroll view. The idea is we will
// perform hit testing every n-pixel in the scroll view to see what table row
// lies there and change its color accordingly
func scrollViewDidScroll(_ notification: Notification) {
// The data's part of a table view begins with at the bottom of the table's header
let topEdge = tableView.headerView!.frame.height
let bottomEdge = scrollView.bounds.height

// We are going to do hit-testing every 10 pixel. For best efficiency, set
// the value to your typical row's height
let step = CGFloat(10.0)

for y in stride(from: topEdge, to: bottomEdge, by: step) {
let point = NSPoint(x: 10, y: y) // the point, in the coordinates of the scrollView
let hitPoint = scrollView.convert(point, to: tableView) // the same point, in the coordinates of the tableView

// The row that lies that the hitPoint
let row = tableView.row(at: hitPoint)

// If there is a row there
if row > -1 {
let rect = tableView.rect(ofRow: row) // the rect that contains row's view
let rowRect = tableView.convert(rect, to: scrollView) // the same rect, in the scrollView's coordinates system
let visibleRect = rowRect.intersection(scrollView.bounds) // the part of the row that visible from the scrollView
let visibility = visibleRect.height / rowRect.height // the percentage of the row that is visible

for column in 0..<tableView.numberOfColumns {
// Now iterate through every column in the row to change their color
if let cellView = tableView.view(atColumn: column, row: row, makeIfNecessary: true) as? NSTableCellView {
let color = cellView.textField?.textColor

// The rows in a typical text-only tableView is 17px tall
// It's hard to spot their grayness so we exaggerate the
// alpha component a bit here:
let alpha = visibility == 1 ? 1 : visibility / 3

cellView.textField?.textColor = color?.withAlphaComponent(alpha)
}
}
}
}
}

结果:

enter image description here

关于swift - 跟踪 NSCell 的位置变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447394/

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