Swift-UI多选删除
来源:互联网 发布:编程入门教程 excel 编辑:程序博客网 时间:2024/05/17 09:10
//
// ViewController.swift
// UITableView-MoreDelete
//
// Created by Qsyx on 16/6/24.
// Copyright © 2016年 Qsyx. All rights reserved.
//
import UIKit
class ViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {
//宏定义
let Width =UIScreen.mainScreen().bounds.size.width
let Height =UIScreen.mainScreen().bounds.size.height
//带参的宏定义
func MakeColor(r :CGFloat, g : CGFloat, b :CGFloat) -> UIColor {
returnUIColor.init(red: r, green: g, blue: b, alpha:1.0)
}
//<1>属性
//UITableView
var tableView :UITableView!
//数据源
var dataArray :NSMutableArray!
//右按钮
var allButton :UIBarButtonItem!
var deleteButton :UIBarButtonItem!
//待删除的数据的数组
var deleteArray :NSMutableArray!
//待删除的cell的数组
var cellArray :NSMutableArray!
//记录全选按钮的点击状态:是否应该执行全选操作,还是全不选的操作
var isAll :Bool = true
overridefunc viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =MakeColor(0.8, g:0.5, b: 0.3)
self.automaticallyAdjustsScrollViewInsets =false
//<1>创建数据源
self.creatDataArray()
//<2>创建UITableView
self.creatTableView()
//<3>创建左右按钮
self.creatButton()
}
// MARK: - 创建数据源
func creatDataArray() ->Void {
//
//<1>开辟内存空间
self.dataArray =NSMutableArray()
self.cellArray =NSMutableArray.init(capacity:0)
self.deleteArray =NSMutableArray.init(capacity:0)
//<2>创建数据源
for iin 1...30 {
dataArray.addObject("这是第\(i)数据")
}
}
// MARK: - 创建UITableView
func creatTableView() ->Void {
//
self.tableView =UITableView.init(frame:CGRectMake(0,64, Width,Height - 64), style:UITableViewStyle.Plain)
self.tableView.delegate =self
self.tableView.dataSource =self
self.view.addSubview(self.tableView)
//注册cell
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "iden")
}
// MARK: - 创建左右按钮
func creatButton() ->Void {
//左按钮
self.navigationItem.leftBarButtonItem = self.editButtonItem()
//删除按钮
self.deleteButton =UIBarButtonItem.init(title:"删除", style:UIBarButtonItemStyle.Done, target:self, action: Selector("deleteButtonClick"))
//全选按钮的创建
self.allButton =UIBarButtonItem.init(title:"全选", style:UIBarButtonItemStyle.Done, target:self, action: Selector("allButtonClick"))
}
// MARK: - 编辑按钮对应的方法
overridefunc setEditing(editing:Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
//editing参数:false -> true -> false
//判断editing的状态
if editing {
self.navigationItem.rightBarButtonItems = [self.deleteButton,self.allButton]
}else{
self.navigationItem.rightBarButtonItems = nil
}
//将deleteArray清空
//cellArray清空
self.deleteArray.removeAllObjects()
self.cellArray.removeAllObjects()
self.isAll =true
//设置UITableView的编辑状态
//self.tableView.editing 能获取到tableView的编辑状态 默认false
self.tableView.setEditing(!self.tableView.editing, animated: true)
// self.tableView.setEditing(editing, animated: true)
}
// MARK: - 一键删除对应的方法
func deleteButtonClick() ->Void {
//
//将deleteArray里面的数据从DataArray里面删掉
self.dataArray.removeObjectsInArray(self.deleteArrayas [AnyObject])
//刷新数据
// self.tableView.reloadData()
//带有动画效果的删除
self.tableView.deleteRowsAtIndexPaths(self.cellArrayas NSArrayas! Array, withRowAnimation:UITableViewRowAnimation.Automatic)
//清空之前的元素
self.deleteArray.removeAllObjects()
self.cellArray.removeAllObjects()
}
// MARK: - 全选按钮对应的方法
func allButtonClick() ->Void {
ifself.isAll {
//全选
//遍历数据源的个数
for rowin 0..<self.dataArray.count {
//获取所有cell的下标
let indexPath =NSIndexPath.init(forRow: row, inSection:0)
//将所有的cell选中
self.tableView.selectRowAtIndexPath(indexPath, animated:true, scrollPosition: UITableViewScrollPosition.None)
//将cell的下标放到cellArray里面
self.cellArray.addObject(indexPath)
}
//将dataArray里面的所有数据放到deleteArray里面
self.deleteArray.addObjectsFromArray(self.dataArrayas [AnyObject])
//将状态置反
self.isAll =false
}else{
//全不选
for rowin 0..<self.dataArray.count {
let index =NSIndexPath.init(forRow: row, inSection:0)
//取消选中
self.tableView.deselectRowAtIndexPath(index, animated:true)
}
//将数组置空
self.deleteArray.removeAllObjects()
self.cellArray.removeAllObjects()
//将状态置反
self.isAll =true
}
}
// MARK: - 基本的代理方法
//设置行数
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
returnself.dataArray.count
}
//UITableViewCell
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
//
let cell = tableView.dequeueReusableCellWithIdentifier("iden", forIndexPath: indexPath)
cell.textLabel?.text =self.dataArray[indexPath.row]as? String
return cell
}
// MARK: - 多选删除相关的代理方法
//设置编辑状态
func tableView(tableView:UITableView, editingStyleForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCellEditingStyle {
// return UITableViewCellEditingStyle.Delete //单选删除
// return UITableViewCellEditingStyle.Insert //新增元素
returnUITableViewCellEditingStyle.init(rawValue:UITableViewCellEditingStyle.Delete.rawValue |UITableViewCellEditingStyle.Insert.rawValue)!
}
//选中事件:将选中的那一行的数据放到deleteArray中
func tableView(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
//
//判断
//当tableView处于编辑状态时
ifself.tableView.editing {
//将选中的那一行的数据放到deleteArray中
self.deleteArray.addObject(self.dataArray[indexPath.row])
//动画效果
//将选中的cell放倒数组里面 cellArray
self.cellArray.addObject(indexPath)
self.isAll =false
}else{
//当不处于编辑状态时:做其他操作
tableView.deselectRowAtIndexPath(indexPath, animated:true)
}
}
//取消选中:将不再取消的数据从deleteArray里面删除
func tableView(tableView:UITableView, didDeselectRowAtIndexPath indexPath:NSIndexPath) {
//
ifself.tableView.editing {
//将不再取消的数据从deleteArray里面删除
self.deleteArray.removeObject(self.dataArray[indexPath.row])
//将不再选中的cell从cellArray里面移除
self.cellArray.removeObject(indexPath)
self.isAll =true
}else{
print("qwe")
}
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
- Swift-UI多选删除
- Swift UI
- Swift 关于tableView的多选和删除
- easy UI 的多选删除参考代码!
- Swift UI控件 --> UITableView
- Swift学习笔记-UI
- UI基础控件 Swift
- swift-UI-01
- Swift--UI--UIView
- Swift--UI--UIButton
- swift UI专项训练19 TextView 多行文本
- Swift - tableView的单元格多选功能的实现(获取多选值、多选删除)
- swift-tableView 删除行
- Swift: 删除子View
- Swift编程 UI控件- UITableView
- swift ui(一) uibutton
- iOS8开发~Swift UI详解
- [ iOS8之Swift~UI详解]
- Sphinx 的介绍和原理探索
- 翻译视频脚本时的注意事项
- 剑指offer题解 翻转单词顺序列
- 工厂模式之--工厂方法模式
- Android studio more than one library with package name 'xxx.xxx.mylibrary'
- Swift-UI多选删除
- 使用Jacob合并多个word文档生成一个word文档
- Nginx开发从入门到精通-基础篇
- Java中的SPI(Service Provider Interface)介绍及示例
- Android:dagger2让你爱不释手-重点概念讲解、融合篇
- 开车遇暴雨请戴墨镜!快转起,很多人会感激你的!
- 黑马程序员:从2016互联网大会数据,看PHP语言发展潜力
- ugui组件添加事件处理的方法
- C#双缓冲实现方法(可防止闪屏)