zoukankan      html  css  js  c++  java
  • UItableView的删除(多选删除)以及UITableViewCell方法重写

    一般在对tableView的数据有编辑操作的时候,会用到tableView的编辑状态,如收藏等需要操作的界面。编辑状态下可以对tableView进行增删改等的一系列操作,这里主要针对多选删除,对选删除的时候会涉及到cell的左侧对选按钮(图片)的设置,需要重写UITableViewCell的setEdting,setHighlighted,setSelected等方法,其中:setEdting只需要设置未选中状态图片;setHighlighted只需要设置高亮状态选中状态图片,非高亮状态不用设置;setSelected需要设置选中和未选中状态的图片

    import UIKit
    
    class CKBaseCell: UITableViewCell {
    
        override func awakeFromNib() {
            super.awakeFromNib()
        }
        
        override func setEditing(_ editing: Bool, animated: Bool) {
            super.setEditing(editing, animated: true)
            
            //编辑状态下只控制tableView编辑状态.none状态的图片
            if editing{
                for control in self.subviews{
                    if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!) {
                        for view in control.subviews{
                            if view.isKind(of: UIImageView.classForCoder()){
                                let image = view as! UIImageView
                                image.image = #imageLiteral(resourceName: "ic_collection_unchecked")
                            }
                        }
                    }
                }
            }
        }
        
        override func setHighlighted(_ highlighted: Bool, animated: Bool) {
            super.setHighlighted(highlighted, animated: animated)
            if highlighted == true {
                contentView.backgroundColor = CKGlobalConst.ColorF4
            } else {
                contentView.backgroundColor = .white
            }
            
            //控制高亮状态的图片
            if isEditing {
                for control in self.subviews{
                    if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!) {
                        for view in control.subviews{
                            if view.isKind(of: UIImageView.classForCoder()){
                                let image = view as! UIImageView
                                //只设置高亮状态就可以,如果设置了else为未选中图片的话,当cell滑出界面或者刷新界面的话,即便cell状态设置为了已选中状态图片也会因为是非高亮状态而显示未选中图片
                                if highlighted {
                                    image.image = #imageLiteral(resourceName: "ic_topic_checked")
                                }
                            }
                        }
                    }
                }
            }
        }
        
        override func setSelected(_ selected: Bool, animated: Bool) {
    //        super.setHighlighted(selected, animated: animated)
            super.setSelected(selected, animated: animated)
            if selected == true {
                contentView.backgroundColor = CKGlobalConst.ColorF4
            } else {
                contentView.backgroundColor = .white
            }
            
            //控制选中状态图片(实例:收藏,选中后刷新或cell滑出界面之后,再出现的时候判断选中数组中是否包含该行cell,包含的话就将该行状态设置为选中状态,只要是选中状态就会走这个方法)
            if isEditing{
                for control in self.subviews{
                    if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!) {
                        for view in control.subviews{
                            if view.isKind(of: UIImageView.classForCoder()){
                                let image = view as! UIImageView
                                if selected{
                                    image.image = #imageLiteral(resourceName: "ic_topic_checked")
                                }else{
                                    image.image = #imageLiteral(resourceName: "ic_collection_unchecked")
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    在tableView中,如果cell滑出了界面或者界面进行刷新了的时候,需要在cellForRow的方法中判断当前cell是否包含于“要删除的数组”,包含于“要删除的数组”的cell设置其setSelected/isSelected为选中状态(True)

    import UIKit
    
    class CKCollectionController: UIViewController, UITableViewDelegate, UITableViewDataSource {
        
        var tableView: UITableView?
        var editButton = UIButton()
        var articleModelArray: [CKArticleModel] = []
        //要删除的文章ID
        var deleteIdArray: [String] = []
        //要删除的cell行数
        var deleteIndexArray: [String] = []
        let promptView = CKPromptView()
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            navigationController?.setNavigationBarHidden(false, animated: true)
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.white
            setNaviBar()
            setupUI()
            loadData()
        }
        
        func setNaviBar() {
            editButton.setTitle("    Edit   ", for: .normal)
            editButton.setTitleColor(UIColor.colorWithHex(hex: 0x35b3fc), for: .normal)
            editButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            editButton.addTarget(self, action: #selector(editClick), for: .touchUpInside)
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: editButton)
            navigationItem.title = "Collection"
        }
        
        func loadData() {
            //第一次加载,显示loding
            if self.articleModelArray.count == 0 {
                //添加loading全局缺省图
                promptView.addDefaultView(self.view, #imageLiteral(resourceName: "loading"), CKMultilingual.loading)
            }
            
            //无网的时候,直接不请求
            if !CKUtil.isConnectedToNetwork() {
                promptView.removeDefaultView()
                if self.articleModelArray.count == 0 {
                    //第一次加载或者整体数据为空,添加全局缺省图
                    promptView.addDefaultView(self.view, #imageLiteral(resourceName: "no network"), CKMultilingual.Network_connection_is_abnormal, CKMultilingual.Click_on_the_screen_to_try_again, #selector(self.loadData))
                }
                return
            }
            
            let collectApi = CKCollectAPI()
            collectApi.callbackSuccess = { api in
                //清除loading
                self.promptView.removeDefaultView()
                
                var modelArray: [CKArticleModel] = []
                modelArray = (api as! CKCollectAPI).articleModelArray
                self.articleModelArray = modelArray
                if self.articleModelArray.count == 0 {
                    //整体没有数据,添加全局缺省图
                    self.promptView.addDefaultView(self.view, #imageLiteral(resourceName: "no content"), CKMultilingual.No_content_yet)
                    self.editButton.setTitleColor(UIColor.gray, for: .normal)
                    self.editButton.isUserInteractionEnabled = false
                }else{
                    self.editButton.setTitleColor(UIColor.blue, for: .normal)
                    self.editButton.isUserInteractionEnabled = true
                }
                self.tableView?.reloadData()
            }
            
            collectApi.callbackFailure = { error in
                print("(error)")
                self.promptView.removeDefaultView()
                if self.articleModelArray.count == 0 {
                    //整体没有数据,添加全局缺省图
                    self.promptView.addDefaultView(self.view, #imageLiteral(resourceName: "no content"), CKMultilingual.No_content_yet)
                }
            }
            
            collectApi.execute()
        }
        
        func setupUI() {
            let tableView = UITableView(frame: .zero, style: .plain)
            tableView.delegate = self
            tableView.dataSource = self
            //编辑状态可多选
            tableView.allowsMultipleSelectionDuringEditing = true
            tableView.estimatedRowHeight = 180
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.showsVerticalScrollIndicator = false
            tableView.backgroundColor = CKGlobalConst.ColorF4
            view.addSubview(tableView)
            self.tableView = tableView
            
            // 注册单图cell
            tableView.register(UINib.init(nibName: "CKNormalCell", bundle: nil), forCellReuseIdentifier: CKGlobalConst.normalCellID)
            // 注册多图图cell
            tableView.register(UINib.init(nibName: "CKMultiImageCell", bundle: nil), forCellReuseIdentifier: CKGlobalConst.multiImageCellID)
            // 注册无图cell
            tableView.register(UINib.init(nibName: "CKNoImageCell", bundle: nil), forCellReuseIdentifier: CKGlobalConst.noImageCellID)
            // 注册大图cell
            tableView.register(UINib.init(nibName: "CKLargeImageCell", bundle: nil), forCellReuseIdentifier: CKGlobalConst.largeImageCellID)
            // 视频cell
            tableView.register(UINib.init(nibName: "CKVideoCell", bundle: nil), forCellReuseIdentifier: "CKVideoCellID")
            
            tableView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
                make.center.equalToSuperview()
            }
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return articleModelArray.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            var reuseID:String = CKGlobalConst.normalCellID
            if (articleModelArray[indexPath.row].images()?.count) ?? 0 >= 3{
                reuseID = CKGlobalConst.multiImageCellID
            } else if (articleModelArray[indexPath.row].images()?.count)  ?? 0 == 0 {
                reuseID = CKGlobalConst.noImageCellID
            } else if articleModelArray[indexPath.row].ctype == 1 {
                reuseID = CKGlobalConst.largeImageCellID
            }
            
            let cell: CKBaseCell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) as! CKBaseCell
            cell.model = articleModelArray[indexPath.row]
            
            //先判断文章是否已阅读
            if UserDefaults.standard.bool(forKey: "isRead(String(describing: cell.model?.articleid))") == true {
                cell.titleLabel.textColor = UIColor.colorWithHex(hex: 0x989898)
            } else {
                cell.titleLabel.textColor = CKGlobalConst.Color2
            }
            
            if deleteIndexArray.count != 0 {
                for index in deleteIndexArray{
                    if (index as NSString).integerValue == indexPath.row{
                        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    //                    cell.setSelected(true, animated: false)
                        cell.isSelected = true
                    }
                }
            }
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if tableView.isEditing {
                let cell: CKBaseCell = tableView.cellForRow(at: indexPath) as! CKBaseCell
                cell.isSelected = true
                //添加要删除cell的信息
                deleteIndexArray.append(String(describing: indexPath.row))
                //添加要删除的文章ID
                deleteIdArray.append(String(describing: articleModelArray[indexPath.row].id ?? 0))
                editButton.setTitle("Delete", for: .normal)
            }else{
                print(articleModelArray[indexPath.row])
                
                tableView.deselectRow(at: indexPath, animated: true)
                let detailController = CKDetailViewController()
                let cell: CKBaseCell = tableView.cellForRow(at: indexPath) as! CKBaseCell
    
                //存储已阅读状态
                UserDefaults.standard.set(true, forKey: "isRead(String(describing: cell.model?.articleid))")
                cell.titleLabel.textColor = UIColor.colorWithHex(hex: 0x989898)
                detailController.articleModel = cell.model
    
                let navigationController: CKNavigationController = UIApplication.shared.keyWindow?.rootViewController as! CKNavigationController
                navigationController.pushViewController(detailController, animated: true)
    
                //MARK: - 数据埋点: 文章id
                CKUtil.setGatherWith("21", "1", "1", nil)
            }
        }
        
        func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
            if tableView.isEditing{
                let cell: CKBaseCell = tableView.cellForRow(at: indexPath) as! CKBaseCell
                cell.isSelected = false
                //删除要取消删除cell的信息
                deleteIndexArray.remove(at: deleteIndexArray.index(of: String(describing: indexPath.row))!)
                //删除要取消删除的文章ID
                deleteIdArray.remove(at: deleteIdArray.index(of: String(describing: articleModelArray[indexPath.row].id ?? 0))!)
                if deleteIndexArray.count == 0 {
                    editButton.setTitle("Cancel", for: .normal)
                }
            }
        }
        
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }
        
        //开始左滑删除操作时候的操作
        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
            editButton.isEnabled = false
            return .delete
        }
        
        //结束左滑删除操作的时候的操作
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
            if editingStyle == UITableViewCellEditingStyle.delete {
                
                let delCollectApi = CKDeleteCollect()
                delCollectApi.articleids = String(describing: articleModelArray[indexPath.row].id ?? 0)
                delCollectApi.callbackSuccess = {api in
                    self.articleModelArray.remove(at: indexPath.row)
                    tableView.deleteRows(at: [indexPath], with: .left)//平滑效果
                }
                delCollectApi.callbackFailure = {api in
                    
                }
                delCollectApi.execute()
            }
            tableView.reloadData()
        }
        
        //左滑删除后的操作
        func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
            editButton.isEnabled = true
        }
        
        func editClick() {
            editButton.isSelected = !editButton.isSelected
            if editButton.isSelected {
                if deleteIndexArray.count != 0{
                    deleteIndexArray.removeAll()
                }
                if deleteIdArray.count != 0{
                    deleteIdArray.removeAll()
                }
                editButton.setTitle("Cancel", for: .normal)
                tableView?.setEditing(true, animated: true)
            }else{
                editButton.setTitle("  Edit  ", for: .normal)
                tableView?.setEditing(false, animated: true)
                
                let delCollectApi = CKDeleteCollect()
                delCollectApi.articleids = deleteIdArray.joined(separator: ",")
                delCollectApi.callbackSuccess = {api in}
                delCollectApi.callbackFailure = {api in}
                delCollectApi.execute()
                
                var delIdArray:[CKArticleModel] = []
                for index in self.deleteIndexArray{
                    delIdArray.append(self.articleModelArray[(index as NSString).integerValue])
                }
                //获取要删除的文章ID
                for model in delIdArray{
                    self.articleModelArray.remove(at: self.articleModelArray.index(of: model)!)
                }
                self.deleteIndexArray.removeAll()
                self.deleteIdArray.removeAll()
                tableView?.reloadData()
            }
        }
        
    }
  • 相关阅读:
    【递归】拆分自然数
    HDOJ3068最长回文
    博弈论——尼姆博奕
    vijos P1047最小公倍数
    Eular质数筛法-hiho一下 第九十三周
    hdoj-5652 India and China Origins二分+bfs
    hdoj-1166排兵布阵 简单的树状数组
    hdoj-5641 king's phone
    hdoj-1548简单的bfs题目
    命令中"|"的意义
  • 原文地址:https://www.cnblogs.com/2362602833-jhr/p/8032251.html
Copyright © 2011-2022 走看看