標籤: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):編輯(增加、刪除、移動)