从表格的一个Cell弹出Popver窗口问题
2015-11-02 00:19
232 查看
最近为了弄明白表格我着手改写apple的一个例子,TableViewPlayground,我将它改写为swift代码的。
这期间学到很多内容,我把我认为有用的分段在这里记录下来。这个Popver就是里面的一项内容。表格里面又一个色块,是一个自定义控件,当我们点击这个控件,就会弹出一个Popver窗口,里面是有名称的色彩表格,你可以选择其中的一个颜色,双击的的话,就会选中那个颜色,然后修改原地的内容,Popver表格关闭。这是个实用的功能。
这里的选择窗口是一个独立的窗口,拥有自己的ViewController和xib文件。
上面是我们控件的代码。我之所以要贴出来,是因为表格的一个事情。这空间加入表格后,它的mouseUp事件缺省是被屏蔽的。它本来要去触发Popver窗口的弹出,但是点击没有反应。你在TableCellView加入的按钮会有响应。具体原因目前我还没有弄明白,后来我总弄明白了怎么让这个mouseup事件触发。
就是表格视图接口的:
通过它,触发可以了。
于是下面两个函数分别被调用:
你看的出来, 我们的弹出选择框是相当独立的,Popver 也被包含在里面。
这里面
内容都在代码里面,不解释了。
这期间学到很多内容,我把我认为有用的分段在这里记录下来。这个Popver就是里面的一项内容。表格里面又一个色块,是一个自定义控件,当我们点击这个控件,就会弹出一个Popver窗口,里面是有名称的色彩表格,你可以选择其中的一个颜色,双击的的话,就会选中那个颜色,然后修改原地的内容,Popver表格关闭。这是个实用的功能。
这里的选择窗口是一个独立的窗口,拥有自己的ViewController和xib文件。
class ATColorView: NSControl { var backgroundColor: NSColor = NSColor.blueColor() var drawBorder: Bool = false override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) // 绘制控件背景 backgroundColor.set() NSRectFill( dirtyRect ) // 这里绘制控件的外框 if self.drawBorder { NSColor.lightGrayColor().set() NSFrameRectWithWidth(self.bounds, 1.0) } // Drawing code here. } override func mouseUp(theEvent: NSEvent) { let point = self.convertPoint( theEvent.locationInWindow,fromView:nil) if NSPointInRect( point, self.bounds ) && self.action != nil { NSApp.sendAction(self.action, to: self.target, from: self) } } }
上面是我们控件的代码。我之所以要贴出来,是因为表格的一个事情。这空间加入表格后,它的mouseUp事件缺省是被屏蔽的。它本来要去触发Popver窗口的弹出,但是点击没有反应。你在TableCellView加入的按钮会有响应。具体原因目前我还没有弄明白,后来我总弄明白了怎么让这个mouseup事件触发。
就是表格视图接口的:
func tableView(tableView: NSTableView, selectionIndexesForProposedSelection proposedSelectionIndexes: NSIndexSet) -> NSIndexSet{ let row = tableView.clickedRow if (row != -1 ) { let cellView = tableViewMain.viewAtColumn(0, row: rowForEditingColor, makeIfNecessary: false ) as! ATTableCellView let windowPoint = NSApp.currentEvent?.locationInWindow let point = cellView.superview?.convertPoint(windowPoint!, fromView: nil) let view = cellView.hitTest(point!) if view is ATColorView { return tableView.selectedRowIndexes } } return proposedSelectionIndexes }
通过它,触发可以了。
于是下面两个函数分别被调用:
func editColorOnRow(row : Int) { rowForEditingColor = row; let cellView = self.tableViewMain.viewAtColumn( 0, row:row, makeIfNecessary: false ) as! ATTableCellView let color = cellView.colorView.backgroundColor ATColorTableController.sharedColorTableController!.delegate = self ATColorTableController.sharedColorTableController!.editColor(color, positioningView:cellView.colorView) } @IBAction func cellColorViewClicked(sender: AnyObject) { // 根据点击的视图获得表格所在的行 let row = self.tableViewMain.rowForView( sender as! NSView ) if row != -1 { editColorOnRow(row) } }
你看的出来, 我们的弹出选择框是相当独立的,Popver 也被包含在里面。
这里面
sharedColorTableController 单列的写法是从网上找到的。感谢网友的贡献。
内容都在代码里面,不解释了。
protocol ATColorTableControllerDelegate: NSObjectProtocol{ func colorTableController(controller: ATColorTableController, color: NSColor, colorName: String ) } class ATColorTableController: NSViewController { static let sharedColorTableController = ATColorTableController(nibName: "ATColorTableController", bundle: nil) var popover : NSPopover? var colorList: NSColorList? var colorNames = [String]() @IBOutlet var tableColorList: NSTableView! weak var delegate: ATColorTableControllerDelegate? var updatingSelection = true private override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?){ super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) colorList = NSColorList(named: "Crayons") colorNames = (colorList?.allKeys)! } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func loadView() { super.loadView() tableColorList.intercellSpacing = NSMakeSize(3, 3) tableColorList.target = self tableColorList.doubleAction = Selector("tableViewAction:") } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. } func selectedColorName() -> String? { let row = tableColorList.selectedRow if( row != -1) { return colorNames[row] } return nil } func selectedColor() ->NSColor { let name = selectedColorName() if name != nil { return (colorList?.colorWithKey(name!))! } return NSColor.whiteColor() } @IBAction func tableViewAction(sender: AnyObject) { popover?.close() if self.delegate!.respondsToSelector(Selector("colorTableController:color:colorName:")) { self.delegate?.colorTableController(self, color: self.selectedColor(), colorName: self.selectedColorName()!) } } func makePopoverIfNeeded(){ popover = NSPopover() popover!.contentViewController = self popover!.behavior = NSPopoverBehavior.Transient popover!.delegate = self } func selectColor( color : NSColor ){ var row = 0 for name in colorNames { let colorIn = colorList?.colorWithKey(name) if colorIn == color { break } row++ } updatingSelection = true // This is done in an animated fashion } func editColor( color : NSColor, positioningView: NSView ) { self.makePopoverIfNeeded() self.selectColor( color ) popover?.showRelativeToRect(positioningView.bounds, ofView: positioningView, preferredEdge: NSRectEdge.MinY) } } extension ATColorTableController: NSPopoverDelegate{ func popoverDidClose(notification: NSNotification){ popover = nil } } extension ATColorTableController:NSTableViewDelegate, NSTableViewDataSource{ func numberOfRowsInTableView(aTableView: NSTableView) -> Int { return self.colorNames.count } func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? { let name = colorNames[row] let color = colorList?.colorWithKey(name) let result = tableView.makeViewWithIdentifier((tableColumn?.identifier)!, owner: nil) as! ATTableCellView result.colorView.backgroundColor = color! result.colorView.drawBorder = true result.subTitleTextField.stringValue = name return result } }
相关文章推荐
- linux 常用基础命令 ls 详细介绍
- dnw2的使用
- CentOS 搭建svn失败,无法连接host解决方法
- linux中安装nfs
- CentOS 6.5 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
- Linux下chkconfig命令详解
- 如何获取OTOY服务器root权限的
- docker在windows上无法跑起来的解决方法
- Storm运维:关于nextTuple
- centos7安装小结
- Linux下rar 命令压缩和解压详解
- linux学习一天一个命令(4)[mkdir命令]
- puppet入门
- Linux ssh-keygen生成公钥和私钥
- locate(每天一个Linux命令-27)
- 《how tomcat works》翻译开篇
- [Gradle]specified for property 'bundle' does not exist.
- linux正则表达式-1
- 【软考之路】测试和维护
- linux--同步时间