当前位置: 代码迷 >> 综合 >> (史上最全版2020.7)Swfit-TableView全选、返选、取消、删除、确认
  详细解决方案

(史上最全版2020.7)Swfit-TableView全选、返选、取消、删除、确认

热度:27   发布时间:2023-11-18 04:06:11.0

本次重点讲述如何使用Swfit语言,在TableView列表上做(全选、返选、编辑、取消、提交确认)功能。

1.我们为什么要做这些功能?

如今互联网移动时代,比如:我们常见使用购物、教学、支付APP等,都需要用到多选以及单选列表,获取到点击的唯一ID,达到实现项目需求。

2.主要功能有哪些??

  • 列表:通过TableView获取到列表信息;字段:如,Name、Email、UUID、MAC地址等
  • 按钮:左上角返回键,右上角编辑+取消键,下方固定左侧(全选+返选),右侧确认键

操作如下:

  • 要点击编辑状态,才可以进行选中列表,否则会给予提示
  • 默认是为编辑(不能选中/全选)-右上角点击编辑,变为取消(可以选中/全选/返选)
  • 全选列表之后,再单击某个列表,就是取消当前的选中
  • 确认:数组列表不能为空,否则给予失败提示

3.效果图:

在这里插入图片描述

二:代码区:

cell!.selectionStyle = .none(千万不要写这个,不然列表不能选中)

 1. 定义的类文件,代理方法,变量的介绍
class PurchaseController: BaseViewController,PurchaseProtocol,PurchaseCellProtocol,PurchaseViewProtocol,UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate{
    var purchaseView = PurchaseView()//view层var purchaseCell = PurchaseCell()//cell层var indexArray :[String] = []//用于接收选中列表后点击确认的数组private var deviceList = Array<PurchaseManList>()//读取到服务器的数组列表private var spage = 1//服务器参数private var spagesize = 10//服务器参数private var purchasePresenter = PurchasePresenter<PurchaseController>()//接口类实现代理private var selectArray = Array<PurchaseManList>()//用于接收选中列表的数组}
	//初始化override func viewDidLoad() {
    super.viewDidLoad()}//标题override func customContentView(){
    navigationController?.navigationBar.barTintColor =  UIColor(hexString: "#353535", transparency: 1.0)navigationController?.navigationBar.tintColor = UIColor.whitenavigationItem.title = "采购订单"}//右边按钮键override func customNavigationRightItem() {
    item = UIBarButtonItem(title:"编辑", style: .plain, target: self, action: #selector(onSetClick))item.tintColor = UIColor.whitenavigationItem.rightBarButtonItem = item}//启动程序的运行时override func viewWillAppear(_ animated: Bool) {
    pad_cancal_ok = "ok"//判断只有在编辑的状态下才可以选中purchasePresenter.initial(self)//设备数据层初始化purchasePresenter.PurchaseListJson(page:spage, pagesize:spagesize)//显示加载出来showHud()}override func initView() {
    let view = UIView()purchaseView.delegate = selfpurchaseCell.delegate = selfpurchaseView.tableView?.delegate = selfpurchaseView.tableView?.dataSource = selfpurchaseView.tableView?.tableFooterView = viewpurchaseView.frame = self.view.boundsself.view.addSubview(purchaseView)//设置允许单元格多选purchaseView.tableView!.register(UITableViewCell.self,forCellReuseIdentifier: PURCHASECONTROLLERTABELTEXT)purchaseView.tableView?.allowsMultipleSelectionDuringEditing = true}override func initData() {
    select = falsepurchaseView.uniselsctAll?.isHidden = truepurchaseView.checkAll?.isHidden = false}

右上角点击按钮事件处理

@objc func onSetClick(){
    if(purchaseView.tableView!.isEditing == false) {
    pad_cancal_ok = "no"item.title = "取消";purchaseView.tableView!.setEditing(true, animated:true)}else {
    pad_cancal_ok = "ok"item.title = "编辑";indexArray.removeAll()purchaseView.tableView!.setEditing(false, animated:true)purchaseView.uniselsctAll?.isHidden = truepurchaseView.checkAll?.isHidden = false}}

得到服务器列表数据的成功/失败的返回

    func getPurchaseSuccess(result: Array<PurchaseManList>) {
    hidHud()if result.count > 0{
    deviceList = resultpurchaseView.tableView?.reloadData()}else{
    WHToast.showMessage("The data is empty.", originY: 500, duration: 2, finishHandler: {
    })}}func getPurchaseFail(result: String) {
    hidHud()WHToast.showMessage(result, originY: 500, duration: 2, finishHandler: {
    })print("失败");}

点击确认按钮的处理

    func onPurchasAffirm() {
    print("wo shi afirm2",indexArray)if indexArray.count > 0 {
    var ids: [String] = []//拿到selectArray里面的model,取id,为ids数组for model in indexArray{
    ids.append(model)}print("indexArray",ids)print("sever",selectArray.count)}}

返选按钮的处理:

 	func onUniselsctAllClick(){
    indexArray.removeAll()//返选删除选中的数组purchaseView.tableView?.reloadData()//刷新列表purchaseView.uniselsctAll?.isHidden = true//隐藏返选purchaseView.checkAll?.isHidden = false//打开全选}

全选按钮的处理:

    func onChckAllClick(){
    //点击全选的时候,判断是不是为编辑的状态if pad_cancal_ok.hasPrefix("ok") {
    print("ok")return}else{
    print("noo")if(purchaseView.tableView!.isEditing == false) {
    purchaseView.uniselsctAll?.isHidden = true//隐藏返选purchaseView.checkAll?.isHidden = false//打开全选}else {
    purchaseView.uniselsctAll?.isHidden = false//打开返选purchaseView.checkAll?.isHidden = true//隐藏全选for i in 0..<deviceList.count {
    print("第\(i)次")let model = deviceList[i]//拿到devicelist列表的索引,取pono唯一订单号ID,进行添加或删除print("(model.pono!)")//把ID添加到数组indexArray.append(model.pono!)// NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];purchaseView.checkAll!.isSelected = !purchaseView.checkAll!.isSelectedif purchaseView.checkAll!.isSelected {
    let count = self.deviceList.countvar indexArray :[IndexPath] = []if count > 0 {
    for i in 0...count - 1{
    let index = IndexPath(row: i, section: 0)indexArray.append(index)}}selectArray.removeAll()//移除现有选择数组的数据selectArray = deviceList//将数据源的所有数据赋值给选择数据for index in indexArray{
    //这个为所有列表循环出来后全选中 purchaseView.tableView?.selectRow(at: index, animated: false, scrollPosition: UITableView.ScrollPosition.none)print("az",index)}}}}}}

UITableView的处理:

cell!.selectionStyle = .none(千万不要写这个,不然列表不能选中)

	//多少个列表数据func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return deviceList.count}func numberOfSections(in tableView: UITableView) -> Int {
    return 1}//cell样式func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell:PurchaseCell? = tableView.dequeueReusableCell(withIdentifier: PURCHASECONTROLLERTABELTEXT) as? PurchaseCellif cell == nil{
    cell = PurchaseCell(style: .default, reuseIdentifier: PURCHASECONTROLLERTABELTEXT)//cell!.selectionStyle = .none}if deviceList.count > 0{
    if indexPath.row < deviceList.count{
    let model = deviceList[indexPath.row]if !cmUtil.isEmpty(model.supplier_Name){
    cell?.deviceNmaeLabel?.text = model.supplier_Name}if !cmUtil.isEmpty(model.pono){
    cell?.deviceTimeLabel?.text = model.pono}}}return cell!;}//列表点击事件func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var cell:PurchaseCell? = tableView.dequeueReusableCell(withIdentifier: PURCHASECONTROLLERTABELTEXT) as? PurchaseCellif cell == nil{
    cell = PurchaseCell(style: .default, reuseIdentifier: PURCHASECONTROLLERTABELTEXT)//cell!.selectionStyle = .none(千万不要写这个,不然列表不能选中)}if(deviceList.count > 0){
    let model = deviceList[indexPath.row]print("vz",model.pono as Any);//判断为true就是tableview选中,返选,为false就是进入详情界面if(purchaseView.tableView!.isEditing == false) {
    let reginserVc = OrderdetailController()reginserVc.codeid = model.pono;self.navigationController?.pushViewController(reginserVc, animated:true)print( "OrderdetailController")}else {
    print("选择当前的列表")indexArray.append(model.pono!)}}}//点击列表取消选中的处理:func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    print("a")var cell:PurchaseCell? = tableView.dequeueReusableCell(withIdentifier: PURCHASECONTROLLERTABELTEXT) as? PurchaseCellif cell == nil{
    cell = PurchaseCell(style: .default, reuseIdentifier: PURCHASECONTROLLERTABELTEXT)//cell!.selectionStyle = .none}if(deviceList.count > 0){
    let model = deviceList[indexPath.row]if indexArray.firstIndex(of: model.pono!) != nil {
    let arr2 = indexArray.filter{
    $0 != model.pono!}print("kkk",arr2);indexArray.removeAll()//删除所有选中的列表数组indexArray.append(contentsOf: arr2)//再进行添加,因为这里每次返选都是取消唯一一个ID,如果不清除所有的,连续返选点击清除,就会依照之前整个indexArray数组,再清除点击的ID}print("hhhhhh",model.pono as Any);}if (purchaseView.tableView!.isEditing == true) {
    return;}purchaseView.tableView?.deselectRow(at: indexPath,animated:true);//Highfunc tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 90}func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //section header Highreturn 18}//section bottom spacefunc tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 20}
}

在这里插入图片描述
整个重点细节都在以上阐述了,大家一起互相学习,刚踏入Swfit,如有写的不好之处,可提出留言,谅解!希望能帮助到你…

  相关解决方案