做者:Arthur Knopper,原文连接,原文日期:2017-01-09
译者:钟颖Cyan;校对:Cwift;定稿:CMBios
经过长按手势来展现上下文菜单,给了用户对选中对象进行 剪切/复制/粘贴 操做的能力。在默认状况下,Table View 的上下文菜单是禁止的。在本文中,使用上下文菜单复制 Table View Cell 上面的文字,随后能够将文字粘贴到 Text Field 里面。本教程基于 Xcode 8.1 和 iOS 10。git
打开 Xcode 并建立一个新的 Single View Application:github
点下一步,用 IOS10ContextMenuTableViewTutorial 做为项目的名字,而后根据你的习惯填写好 Organization Name 和 Organization Identifier。选择 Swift 做为开发语言,而且确保 Devices 为仅 iPhone。swift
打开 Main.storyboard 文件并从 Object Library 拖拽一个 Table View 到 main View 的顶部。打开 Attribute Inspector 在 Table View 区域将 Prototype Cells 设置成 1。数组
选择 Table View Cell 后打开 Attribute Inspector,在 Table View Cell 区域将 Identifier 设置成 "cell"。spa
选择 Table View,点击 storyboard 右下角的固定按钮固定 Table View 的上边、左边和右边。同时选择高度属性给 Table View 设置一个固定的高度。在 Update Frames 的下拉菜单中选择 Items of New Constraints,而后点击 Add 4 Constraints。翻译
从 Object Library 拖拽一个 Text Field 并放到 Table View 的正下方。按住 Ctrl 从 Text Field 内部连线到 Table View,保持按住 Ctrl 的状态并选择 "Vertical Spacing" 和 "Center Horizontally"。代理
选择 Text Field,点击 storyboard 右下角的固定按钮固定 Text Field 的左右边距。code
须要 View Controller 做为 Table View 的代理,选择 TableView,按住 Ctrl 拖拽到 main View 顶部的 View Controller 图标,选择 dataSource,重复一遍并选择 delegate。orm
对 Text Field 进行一样的操做,让 View Controller 成为它的代理。打开 ViewController.swift 文件并将类声明改为:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
添加下面的成员:
var pasteBoard = UIPasteboard.generalPasteboard() var tableData: [String] = ["dog","cat","fish"]
pasteBoard
将用来进行复制粘贴操做,tableData
用来存储展现到 Table View Cells 上面的内容。下一步,修改 Table View 的代理方法:
func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableData.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = tableData[indexPath.row] return cell }
Table View 将会显示 TableData 数组里面的三个内容。为了启用上下文菜单,必须实现下面三个方法:
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool { if (action == #selector(UIResponderStandardEditActions.copy(_:))) { return true } return false } func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) { let cell = tableView.cellForRow(at: indexPath) pasteBoard.string = cell!.textLabel?.text }
为了长按 Table View Cell 的时候显示菜单,tableView:shouldShowMenuForRowAt
方法必须返回 true
。在 tableView:canPerformAction:forRowAt
方法里仅仅显示复制
这个菜单项。在 tableView:performAction:forRowAt:withSender
方法里面将选中的文字复制到剪贴板。
最后,实现 textFieldShouldReturn
方法,当用户编辑 Text Field 时按下回车后收起键盘:
func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.view.endEditing(true) return false }
编译并运行项目,长按列表项并选择复制,将文字粘贴到文本框。
你能够在 ioscreator 的 GitHub 仓库里面找到 IOS10ContextMenuTableViewTutorial 的源码。
本文由 SwiftGG 翻译组翻译,已经得到做者翻译受权,最新文章请访问 http://swift.gg。