【精】表格(UITableView)总结(4):编辑(增加、删除、移动)

转载请声明出处:

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 = leftButtonrightButton = UIBarButtonItem(title: "编辑", style: UIBarButtonItemStyle.Plain, target: self, action: "editAction")self.navigationItem.rightBarButtonItem = rightButtontableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)tableView.allowsSelectionDuringEditing = true // 编辑状态下允许选中行tableView.delegate = selftableView.dataSource = selfself.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? UITableViewCellif(cell == nil) {cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")}cell!.textLabel?.text = data.objectAtIndex(indexPath.row) as? Stringreturn cell!}// 选中行操作func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {if(tableView.editing) {let cell = tableView.cellForRowAtIndexPath(indexPath)let param = cell!.textLabel?.textprintln(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.rowlet toRow = destinationIndexPath.rowvar 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):编辑(增加、删除、移动)

相关文章:

你感兴趣的文章:

标签云: