【精】表格(UITableView)總結(4):編輯(增加、刪除、移動)

來源:互聯網
上載者:User

標籤:swift   uitableview   編輯   刪除   移動   

轉載請聲明出處:http://blog.csdn.net/jinnchang/article/details/45934781

1、前言

移動(order)實現以下方法:

// 設定哪些行可以被移動func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool// 設定移動操作func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
編輯(delete、add)實現以下方法:
// 設定哪些行可以被編輯func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool    // 設定編輯模式下每行左邊顯示的按鈕樣式func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle    // 設定編輯操作(刪除、插入)func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)    // 設定滑動操作顯示字樣func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String!
2、示範樣本


3、示範代碼

////  ViewController.swift//  UITableViewSample-Editor////  Created by jinnchang on 15/5/21.//  Copyright (c) 2015年 Jinn Chang. All rights reserved.//import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {        var tableView:      UITableView!        var leftButton:     UIBarButtonItem!    var rightButton:    UIBarButtonItem!        var data:           NSMutableArray!        override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                self.title = "歌手名單"                data = ["王力宏","五月天","周杰倫"]                leftButton = UIBarButtonItem(title: "新增", style: UIBarButtonItemStyle.Plain, target: self, action: "addAction")        self.navigationItem.leftBarButtonItem = leftButton                rightButton = UIBarButtonItem(title: "編輯", style: UIBarButtonItemStyle.Plain, target: self, action: "editAction")        self.navigationItem.rightBarButtonItem = rightButton                tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)        tableView.allowsSelectionDuringEditing = true // 編輯狀態下允許選中行        tableView.delegate = self        tableView.dataSource = self                self.view.addSubview(tableView)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }    // 設定每個分段對應的行數    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return data.count    }        // 設定每行的具體內容    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell        if(cell == nil) {            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")        }        cell!.textLabel?.text = data.objectAtIndex(indexPath.row) as? String        return cell!    }        // 選中行操作    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        if(tableView.editing) {            let cell = tableView.cellForRowAtIndexPath(indexPath)            let param = cell!.textLabel?.text            println(param)        }        tableView.deselectRowAtIndexPath(indexPath, animated: true)    }        // 設定哪些行可以被移動    func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {        return true    }        // 設定移動操作    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {        let fromRow = sourceIndexPath.row        let toRow = destinationIndexPath.row                var obj: AnyObject = data.objectAtIndex(fromRow)        data.removeObjectAtIndex(fromRow)        data.insertObject(obj, atIndex: toRow)    }        // 設定哪些行可以被編輯    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {        return true    }        // 設定編輯模式下每行左邊顯示的按鈕樣式    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {        return UITableViewCellEditingStyle.Delete    }        // 設定編輯操作(刪除、插入)    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {        if(editingStyle == UITableViewCellEditingStyle.Delete) {            data.removeObjectAtIndex(indexPath.row)            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)        }    }        // 設定滑動操作顯示字樣    func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String! {        return "刪除"    }        /// 【編輯】按鈕響應事件    func editAction() {        if(editingState()) {            rightButton.title = "編輯"            tableView.setEditing(false, animated: true)        } else {            rightButton.title = "完成"            tableView.setEditing(true, animated: true)        }    }        /// 判斷當前是否處於編輯狀態    func editingState() -> Bool {        return rightButton.title == "完成"    }        /// 【添加】按鈕響應事件    func addAction() {        data.insertObject("神秘歌手", atIndex: data.count)        var indexPath = NSIndexPath(forRow: data.count - 1, inSection: 0)        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom)    }    }
Github上項目地址:https://github.com/jinnchang/SwiftSamples/blob/master/UITableViewSample-Editor4、結語

文章最後更新時間:2015年5月23日15:59:57

【精】表格(UITableView)總結(4):編輯(增加、刪除、移動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.