Table View 上下文菜單教程

做者: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

image

點下一步,用 IOS10ContextMenuTableViewTutorial 做爲項目的名字,而後根據你的習慣填寫好 Organization Name 和 Organization Identifier。選擇 Swift 做爲開發語言,而且確保 Devices 爲僅 iPhone。swift

image

打開 Main.storyboard 文件並從 Object Library 拖拽一個 Table View 到 main View 的頂部。打開 Attribute Inspector 在 Table View 區域將 Prototype Cells 設置成 1。數組

image

選擇 Table View Cell 後打開 Attribute Inspector,在 Table View Cell 區域將 Identifier 設置成 "cell"spa

image

選擇 Table View,點擊 storyboard 右下角的固定按鈕固定 Table View 的上邊、左邊和右邊。同時選擇高度屬性給 Table View 設置一個固定的高度。在 Update Frames 的下拉菜單中選擇 Items of New Constraints,而後點擊 Add 4 Constraints。翻譯

image

從 Object Library 拖拽一個 Text Field 並放到 Table View 的正下方。按住 Ctrl 從 Text Field 內部連線到 Table View,保持按住 Ctrl 的狀態並選擇 "Vertical Spacing" 和 "Center Horizontally"。代理

image

選擇 Text Field,點擊 storyboard 右下角的固定按鈕固定 Text Field 的左右邊距。code

image

須要 View Controller 做爲 Table View 的代理,選擇 TableView,按住 Ctrl 拖拽到 main View 頂部的 View Controller 圖標,選擇 dataSource,重複一遍並選擇 delegate。orm

image

對 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
}

編譯並運行項目,長按列表項並選擇複製,將文字粘貼到文本框。

image

你能夠在 ioscreator 的 GitHub 倉庫裏面找到 IOS10ContextMenuTableViewTutorial 的源碼。

本文由 SwiftGG 翻譯組翻譯,已經得到做者翻譯受權,最新文章請訪問 http://swift.gg

相關文章
相關標籤/搜索