Table View 上下文菜單教程

作者:Arthur Knopper,原文鏈接,原文日期:2017-01-09
譯者:鐘穎Cyan;校對:Cwift;定稿:CMB

通過長按手勢來展示上下文菜單,給了用戶對選中對象進行 剪切/復制/粘貼 操作的能力。在默認情況下,Table View 的上下文菜單是禁止的。在本文中,使用上下文菜單復制 Table View Cell 上面的文字,隨后可以將文字粘貼到 Text Field 里面。本教程基于 Xcode 8.1 和 iOS 10。

打開 Xcode 并創(chuàng)建一個新的 Single View Application:

image
image

點下一步,用 IOS10ContextMenuTableViewTutorial 作為項目的名字,然后根據(jù)你的習慣填寫好 Organization Name 和 Organization Identifier。選擇 Swift 作為開發(fā)語言,并且確保 Devices 為僅 iPhone。

image
image

打開 Main.storyboard 文件并從 Object Library 拖拽一個 Table View 到 main View 的頂部。打開 Attribute Inspector 在 Table View 區(qū)域?qū)?Prototype Cells 設(shè)置成 1。

image
image

選擇 Table View Cell 后打開 Attribute Inspector,在 Table View Cell 區(qū)域?qū)?Identifier 設(shè)置成 "cell"。

image
image

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

image
image

從 Object Library 拖拽一個 Text Field 并放到 Table View 的正下方。按住 Ctrl 從 Text Field 內(nèi)部連線到 Table View,保持按住 Ctrl 的狀態(tài)并選擇 "Vertical Spacing" 和 "Center Horizontally"。

image
image

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

image
image

需要 View Controller 作為 Table View 的代理,選擇 TableView,按住 Ctrl 拖拽到 main View 頂部的 View Controller 圖標,選擇 dataSource,重復一遍并選擇 delegate。

image
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 上面的內(nèi)容。下一步,修改 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 數(shù)組里面的三個內(nèi)容。為了啟用上下文菜單,必須實現(xiàn)下面三個方法:

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 方法里面將選中的文字復制到剪貼板。

最后,實現(xiàn) textFieldShouldReturn 方法,當用戶編輯 Text Field 時按下回車后收起鍵盤:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

編譯并運行項目,長按列表項并選擇復制,將文字粘貼到文本框。

image
image

你可以在 ioscreator 的 GitHub 倉庫里面找到 IOS10ContextMenuTableViewTutorial 的源碼。

本文由 SwiftGG 翻譯組翻譯,已經(jīng)獲得作者翻譯授權(quán),最新文章請訪問 http://swift.gg

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容