創作內容

2 GP

[Swift筆記]TableViewCell左滑刪除按鈕

作者:kamiisland│2018-01-17 14:53:51│巴幣:4│人氣:1145

1.實作TableViewCell左滑顯示刪除按鈕   

    /**
     * 實作TableView方法,自動出現左滑功能
    **/
    func tableView(_ tableView: UITableView,
                            commit editingStyle: UITableViewCellEditingStyle,
                            forRowAt indexPath: IndexPath)
    {
        let select:Int = indexPath.row
        //=========
        // 操作
        //=========        
        tableView.reloadData() // 更新tableView       
    }
    
    /**
     * 自訂delete的文字為刪除
     */
    func tableView(_ tableView: UITableView,
                            titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath)
            -> String?
    {
        return "刪除"
    }


2.實作TableViewCell左滑顯示多個按鈕   

    /**
     * 實作TableView方法,自動出現左滑功能,並進入編輯模式
     * 編輯模式 => tableView.isEditing = true
    **/
    func tableView(_ tableView: UITableView,
                             commit editingStyle: UITableViewCellEditingStyle,
                             forRowAt indexPath: IndexPath) {
    }
    
    /**
     * 實作TableView回傳Action Array的方法,來自定左滑後顯示的按鈕及動作
    **/
    func tableView(_ tableView: UITableView,
                            editActionsForRowAt indexPath: IndexPath)
            -> [UITableViewRowAction]?
    {

        var actionArr:Array<UITableViewRowAction> = [UITableViewRowAction]()

        // 建立編輯按鈕
        let actionEdit:UITableViewRowAction =
             UITableViewRowAction(style: UITableViewRowActionStyle.normal,
                                                    title: "編輯") { (action, indexPath) in
            let select:Int = indexPath.row            
            //=========
            // 編輯操作
            //=========            
            tableView.isEditing = false; // 退出編輯模式
            tableView.reloadData() // 更新tableView
        }

        
// 建立刪除按鈕
        let actionDelete:UITableViewRowAction =
             UITableViewRowAction(style: UITableViewRowActionStyle.default,
                                                    title: "刪除") { (action, indexPath) in
            let select:Int = indexPath.row            
            //=========
            // 刪除操作
            //=========            
            tableView.isEditing = false; // 退出編輯模式
            tableView.reloadData() // 更新tableView
        }
        
        // 將按鈕動作加入Array,並回傳
        actionArr.append(actionEdit)
        actionArr.append(actionDelete)

        return actionArr;
    }
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3857392
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:Swift|TableViewCell|左滑|刪除|TableView|Swift3|Swift4

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

2喜歡★kamiisland 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:[Swift筆記]取得現... 後一篇:[Swift筆記]顯示含...

追蹤私訊切換新版閱覽

作品資料夾

a86189642祝福
祝大家幸福。看更多我要大聲說昨天22:36


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】