您的位置:首页 > 产品设计 > UI/UE

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

2015-05-23 15:23 573 查看
转载请声明出处:/article/8110375.html

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 UIKit

class 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-Editor

4、结语

文章最后更新时间:2015年5月23日15:59:57
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: