import UIKit
class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
let DeviceWidth = UIScreen.main.bounds.width
let DeviceHeight = UIScreen.main.bounds.height
//数据源数组
var dataArray: NSMutableArray?
//表格
// var tbView: UITableView?
//删除的数组
lazy var deleteArray = NSMutableArray()
lazy var tbView: UITableView? = {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 400, height: 700), style: .plain)
self.automaticallyAdjustsScrollViewInsets = false
tableView.delegate = self
tableView.dataSource = self
return tableView
}()
//创建数据源数组
func createDataArray(){
self.dataArray = NSMutableArray()
for i in 0..<50 {
let model = StudentModel()
//名字
model.name = "第(i+1)个学生"
//年龄
model.age = Int(arc4random()%10)+20
self.dataArray?.add(model)
}
}
override func viewDidLoad() {
super.viewDidLoad()
//1.数据源数组
self.createDataArray()
//2.表格视图
// self.createTableView()
self.view.addSubview(self.tbView!)
//3.遵守协议,实现代理方法
//导航上面添加删除按钮
let rightBarBtn = UIBarButtonItem(title: "删除", style: .done, target: self, action: #selector(deleteAction(_:)))
self.navigationItem.rightBarButtonItem = rightBarBtn
}
func deleteAction(_ barBtn: UIBarButtonItem){
let title = barBtn.title
if title == "删除" {
//进入多行删除的界面
self.tbView?.setEditing(true, animated: true)
barBtn.title = "完成"
}else{
//删除数据
self.dataArray?.removeObjects(in: self.deleteArray as [AnyObject])
//清空删除数组里面的内容
self.deleteArray.removeAllObjects()
//刷新表格
self.tbView?.reloadData()
//结束编辑的状态
self.tbView?.setEditing(false, animated: true)
barBtn.title = "删除"
}
}
//表格视图
func createTableView(){
//导航
self.automaticallyAdjustsScrollViewInsets = false
self.tbView = UITableView(frame: CGRect(x: 0, y: 64, width: DeviceWidth, height: DeviceHeight-64), style: .plain)
self.tbView?.delegate = self
self.tbView?.dataSource = self
self.view.addSubview(self.tbView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//MARK: UITableView代理
extension ViewController{
//返回多少行
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (self.dataArray?.count)!
}
//返回每一行的视图对象
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//重用标志
let cellId = "cellId"
//从重用队列里面获取可重用的cell
var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
//如果没有,创建新的cell对象
if nil == cell {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
}
//显示数据
let model = self.dataArray![indexPath.row] as! StudentModel
cell?.textLabel?.text = model.name
cell?.detailTextLabel?.text = "(model.age!)"
return cell!
}
// 进入多行删除的状态
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle(rawValue: UITableViewCellEditingStyle.delete.rawValue | UITableViewCellEditingStyle.insert.rawValue)!
}
//选择cell的方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//在编辑状态才可以修改数组
if tableView.isEditing == true {
let model = self.dataArray![indexPath.row] as! StudentModel
if ((self.deleteArray.contains(model)) == false) {
//添加到删除数组
self.deleteArray.add(model)
}
}
}
//取消选择
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if tableView.isEditing == true {
let model = self.dataArray![indexPath.row] as! StudentModel
if self.deleteArray.contains(model) {
self.deleteArray.remove(model)
}
}
}
}
网友评论