首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

8.3、UITableView增删改查

2024-12-15 来源:花图问答

//
// ViewController.swift
// DealTableView
//
// Created by modai on 2017/8/4.
// Copyright © 2017年 huanqiu. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//数据源
var dataArray: NSMutableArray?
//表格
var tbView: UITableView?
//数据源[[[],[],[],[],[],[]],[]]
func createDataArray(){
self.dataArray = NSMutableArray()
//男生数组
let boyArray = NSMutableArray()
for i in 0..<5 {
let model = StudentModel()
//名字
model.name = "第(i+1)个男生"
//年龄
model.age = 20+i
boyArray.add(model)
}
self.dataArray?.add(boyArray)
//女生数组
let girlArray = NSMutableArray()
for i in 0..<10 {
let model = StudentModel()
//姓名
model.name = "第(i+1)个女生"
//年龄
model.age = 18+Int(arc4random()%10)
girlArray.add(model)
}
self.dataArray?.add(girlArray)
}
override func viewDidLoad() {
super.viewDidLoad()
//1、数据源
self.createDataArray()
//2.表格
self.createTableView()
//3.遵守协议,实现代理方法
//导航上面添加一个按钮
let rightBtn = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(editAction))
self.navigationItem.rightBarButtonItem = rightBtn
}
//移动的操作
func editAction(){
self.tbView?.isEditing = !(self.tbView?.isEditing)!
}
let DeviceWidth = UIScreen.main.bounds.width
let DeviceHeight = UIScreen.main.bounds.height
//表格视图
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!)
}
//返回多少个分组
func numberOfSections(in tableView: UITableView) -> Int {
return (self.dataArray?.count)!
}
//返回每一组有多少行
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//首先获取该分组数据源子数组
return (self.dataArray![section] as! NSMutableArray).count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "第(section)组"
}
//返回每一行显示的视图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//1.重用标志
let cellId = "cellId"
//2.从重用队列获取可以重用的cell
var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
//3.获取不到,创建新的cell对象
if nil == cell {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
}
//获取数据
let sectionArray = self.dataArray![indexPath.section] as! NSArray
let model = sectionArray[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 .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete {
//1.删除数据源数组里面的一项
let sectionArray = self.dataArray![indexPath.section] as! NSMutableArray
//删除
sectionArray.removeObject(at: indexPath.row)
//添加一个动画
tableView.deleteRows(at: [indexPath], with: .fade)
//刷新表格
// self.tbView?.reloadData()
}else if editingStyle == .insert{
let sectionArray = self.dataArray![indexPath.section] as! NSMutableArray
let model = StudentModel()
model.age = 1
model.name = "aixi"
sectionArray.insert(model, at: indexPath.row+1)
self.tbView?.reloadData()
}
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "删除"
}
//---------------------删除-------------------------
//---------------------移动-------------------------
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
//1.如果两个数据是在同一个section
if sourceIndexPath.section == destinationIndexPath.section {
//修改数据源数组
//移动之前的
let sectionArray = self.dataArray![sourceIndexPath.section] as! NSMutableArray
let model = sectionArray[sourceIndexPath.row] as! StudentModel

        sectionArray.removeObject(at: sourceIndexPath.row)
        sectionArray.insert(model, at: destinationIndexPath.row)
    }else{
        /*
         实际开发中可能不允许男生组的数据移动到女生组
         */
         //2.如果不在同一个section
         //source
         let sourceArray = self.dataArray![sourceIndexPath.section] as! NSMutableArray
         let destArray = self.dataArray![destinationIndexPath.section] as! NSMutableArray
         
         let sourceModel = sourceArray[sourceIndexPath.row] as! StudentModel
         //移动前的位置
         sourceArray.removeObject(at: sourceIndexPath.row)
         //移动后的位置
         destArray.insert(sourceModel, at: destinationIndexPath.row)
    }
}
//返回将要移动到的位置(NSIndexPath)
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
    //如果在同一个section,可以移动
    if sourceIndexPath.section == proposedDestinationIndexPath.section {
        return proposedDestinationIndexPath
    }else{
        //如果是不同的section,不允许移动
        return sourceIndexPath
    }
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

显示全文