gpt4 book ai didi

macos - NSTableView 和 NSNotificationCenter 奇怪的行为

转载 作者:行者123 更新时间:2023-11-28 05:34:47 24 4
gpt4 key购买 nike

在通知观察器中重新加载 NSTableView 数据时,我遇到了非常奇怪的行为。

class MainWindowController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate
{
var data: String[] = []

@IBOutlet var filesTableView: NSTableView!

override func awakeFromNib()
{
super.awakeFromNib()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "droppedFiles:", name: DroppedFilesNotification.notificationName, object: nil)
}

func droppedFiles(notification: NSNotification!)
{
data += ["123"]
println(data.count)

filesTableView.reloadData()
}

func numberOfRowsInTableView(tableView: NSTableView!) -> Int
{
return data.count
}

@IBAction func crazyTest(AnyObject)
{
NSNotificationCenter.defaultCenter().postNotificationName(DroppedFilesNotification.notificationName, object: self, userInfo: [DroppedFilesNotification.fileNamesParameterName: ["123"]])
}
}

第一次调用 crazyTest 函数显示:

1

第二次调用 crazyTest 函数显示:

2
3
4

第三次调用 crazyTest 函数显示数字 5-13。

如果我们从 droppedFiles 函数中删除 filesTableView.reloadData() ,那么除了表格 View 没有更新外,一切正常。知道为什么会发生这种情况以及如何在那里重新加载 TableView 吗?

编辑:

此外,直接调用 droppedFiles 函数而不是使用 NSNotificationCenter 也没有问题。但我更愿意在我的应用程序中使用通知中心。

提前致谢。

最佳答案

如果这是一个基于 View 的 TableView (可能隐含地)从 NIB 加载其 View ,则每次加载 NIB 时都会调用其 awakeFromNib 方法。来自 here :

Note: Calling makeViewWithIdentifier:owner: causes awakeFromNib to be called multiple times in your app. This is because makeViewWithIdentifier:owner: loads a NIB with the passed-in owner, and the owner also receives an awakeFromNib call, even though it’s already awake.

在您的情况下,您每次都在注册通知。因此,您要多次注册,并且每次注册都会收到一次通知。

关于macos - NSTableView 和 NSNotificationCenter 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358621/

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