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

转载请声明出处: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 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

时间: 2025-01-02 17:26:17

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

C# 如何在Excel表格中插入、编辑和删除批注

概述 为文档添加必要的批注可以给文档使用者提供重要的提示信息,下面的示例中,将介绍通过C#编程语言来给Excel表格中的指定单元格内容添加批注,此外,对于已有的批注,如果需要修改,我们也可以进行编辑或者删除批注.示例内容将包含以下主要内容:1.插入批注1.1 插入文本1.2 插入图片2.编辑批注2.1 修改批注内容2.1 设置批注可见性3.删除批注 工具 Spire.XLS for .NET 8.0 提示:在进行代码操作之前,需下载安装Spire.Xls,并添加引用dll文件,添加如下using

JS实现可编辑的表格,双击可编辑,可以删除行和列,增加行和列,重置,导出表格,也可以上下移动元素

JS实现可编辑的表格,双击可编辑,可以删除行和列,增加行和列,重置,导出表格,也可以上下移动元素  原文:JS实现可编辑的表格,双击可编辑,可以删除行和列,增加行和列,重置,导出表格,也可以上下移动元素 源代码下载地址:http://www.zuidaima.com/share/1550463322606592.htm 源代码截图: JS实现可编辑的表格,双击可编辑,可以删除行和列,增加行和列,重置,导出表格,也可以上下移动元素

UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataArray = [NSMutableArray arrayWithArray: @[@"1",@"2",@"3",@"4",@"5",@"6"

基于jQuery表格增加删除代码

基于jQuery表格增加删除代码是一款动态增加删减表格行特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div style="width:720px;margin:20px auto;"> <table id="tab11" style="display: none"> <tbody> <tr> <td height="30" align=&

三.jquery.datatables.js表格编辑与删除

1.为了使用如图效果(即将按钮放入行内http://www.datatables.net/examples/ajax/null_data_source.html) 采用了另一个数据格式 2.后台php,取表格数据变为: public function initable(){ $db = M('yanfa_project')->select(); // 取$db的长度 // $len =count($db); $item=array(); // 循环将$db二维数组每一项的value取出放一个数组

UITableView 自带编辑删除 自定义按钮

一:UITableView 自带编辑删除 1:实现两个方法即可 #pragma mark   tableView自带的编辑功能 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //方法实现后,默认实现手势滑动删除的方法 if (editingStyle!

C# 实现对PPT插入、编辑、删除表格

在现代学习和办公当中,经常会接触到对表格的运用,像各种单据.报表.账户等等.在PPT演示文稿中同样不可避免的应用到各种数据表格.对于在PPT中插入表格,我发现了一个新方法,不过我用到了一款免费的.NET组件--Free Spire.Presentation,在C#中添加该产品DLL文件,可以简单快速地实现对演示文稿的表格插入.编辑和删除等操作.有需要的话可以在下面的网址下载:https://www.e-iceblue.cn/Downloads/Free-Spire-Presentation-NE

[JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改

上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管理的ManageServlet. 接着我们需要一个展现数据的页面,也就是 UserList.jsp <%@page import="com.babybus.sdteam.vo.Student"%> <%@ page language="java" im

[Asp.net MVC]Asp.net MVC5系列——实现编辑、删除与明细信息视图

目录 概述 实现信息的明细视图 实现信息的编辑视图 实现信息的删除视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 [Asp.net MVC]Asp.net MVC5系列——添加模型 [Asp.net MVC]Asp.net MVC5系列——从控制器访问模型中的数据 [Asp.net MVC]Asp.net MVC5系列——添加数据 [Asp.net MVC]Asp.net MVC5系列

UI学习笔记---第十天UITableView表视图编辑

UITableView表视图编辑 表视图编辑的使用场景 当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话 当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置 代理AppDelegate.m中代码 #import "AppDelegate.h" #import "RootViewController.h" @implementation AppDel