加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – OSX:基于视图的表视图中的对象可能只能连接到表视图的

发布时间:2020-12-14 05:24:27 所属栏目:百科 来源:网络整理
导读:我已经设置了一个带有嵌入式NSTableView的NSView. 我试图为对表视图单元格进行更改时运行NSTableViewCell的操作: import Cocoaclass MyView: NSView{ override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) } @IBAction func valEntered2
我已经设置了一个带有嵌入式NSTableView的NSView.

我试图为对表视图单元格进行更改时运行NSTableViewCell的操作:

import Cocoa

class MyView: NSView
{
    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    @IBAction func valEntered2(sender: AnyObject)
    {
        Swift.print("valueEntered2")
    }
}

虽然这种方法在故事板上使用NSViewController之前已经运行良好,但是当我编译此代码时它会发出警告:

由’Table View Cell’发送的动作’valEntered2:’连接到’View’,一个无效的目标(基于视图的对象内部表视图,它们只连接到表视图的委托.)

所以它似乎不喜欢NSView中的动作所以我已经将NSTableView子类化了,所以我可以将它移动到那里:

class MyTable: NSTableView,NSTableViewDataSource,NSTableViewDelegate
{
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

    override init(frame frameRect: NSRect)
    {
        super.init(frame: frameRect)
    }

    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
        self.setDataSource(self)
        self.setDelegate(self)
    }

    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int
    {
        return 5
    }

    func tableView(tableView: NSTableView,viewForTableColumn tableColumn: NSTableColumn?,row: Int) -> NSView?
    {
        let cell: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier,owner: self) as! NSTableCellView
        cell.textField!.stringValue = "Hello World"
        return cell
    }
}

我已在视图中设置表以使用此类:

我本来希望这是这个NSTableView的委托,并能够将TableViewCell的动作添加到MyTable,但它拒绝创建动作.

我错过了哪些步骤?

首先,你必须在tableView单元格中添加一个委托
import Cocoa
protocol cellAction {
  func CellAction()
}

class MyView: NSView
{
var delegate:cellAction!
    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    @IBAction func valEntered2(sender: AnyObject)
    {
      delegate.CellAction()
        Swift.print("valueEntered2")
    }
}

than after that you have to add delegate to the table view

class MyTable: NSTableView,NSTableViewDelegate,cellAction
{
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

    override init(frame frameRect: NSRect)
    {
        super.init(frame: frameRect)
    }

    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
        self.setDataSource(self)
        self.setDelegate(self)
    }

    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int
    {
        return 5
    }

    func tableView(tableView: NSTableView,owner: self) as! NSTableCellView
        cell.textField!.stringValue = "Hello World"
       cell.delegate = self
        return cell
    }
}

in that class you have to call a method in delegate

func CellAction(){
 //here you get the call back 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读