作者:Arthur Knopper,原文鏈接,原文日期:2017-01-09
譯者:鐘穎Cyan;校對:Cwift;定稿:CMB
通過長按手勢來展示上下文菜單,給了用戶對選中對象進行 剪切/復制/粘貼 操作的能力。在默認情況下,Table View 的上下文菜單是禁止的。在本文中,使用上下文菜單復制 Table View Cell 上面的文字,隨后可以將文字粘貼到 Text Field 里面。本教程基于 Xcode 8.1 和 iOS 10。
打開 Xcode 并創建一個新的 Single View Application:
點下一步,用 IOS10ContextMenuTableViewTutorial 作為項目的名字,然后根據你的習慣填寫好 Organization Name 和 Organization Identifier。選擇 Swift 作為開發語言,并且確保 Devices 為僅 iPhone。
打開 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"。
選擇 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 的左右邊距。
需要 View Controller 作為 Table View 的代理,選擇 TableView,按住 Ctrl 拖拽到 main View 頂部的 View Controller 圖標,選擇 dataSource,重復一遍并選擇 delegate。
對 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。