微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在tableview中选择其中之一时如何禁用其他行?

如何解决在tableview中选择其中之一时如何禁用其他行?

我只想选择特定的部分,意味着假设选择了第 1 行,如果我选择了第 2 节的任何行,则删除第 1 行。

以及如何从 var selectedIngredients : Set = [] 获取行值?

SectionTableviewController.swift

//
//  SectionTableViewController.swift
//  SectionTableSearchDemo
//

import UIKit


class SectionTableViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchControllerDelegate {
    
    
    var selectedIngredients : Set<IndexPath> = []
    var searchedSkillsResults = [String]()
    //    var objectFilter = [Skill]()  //
    var filteredobjects = [[Skill]]()
    var products : [Category] = []
    //    var skillCat : [Skill] = []
    
    //var catSkill : Category?
    
    var isSearching = false
    var cat : Welcome?
    var cate : Category?
    var searchController : UISearchController!
    var filterData : [String]!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        parseJSON()
        searchControllerSetup()
    }
    
    
    private func searchControllerSetup(){
        
        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.delegate = self
        
        navigationItem.searchController = searchController
        searchController.obscuresBackgroundDuringPresentation = false
        navigationItem.hidesSearchBarWhenScrolling = false
    }
    
    

    // i don't kNow how to implement and show only skills when search
    func updateSearchResults(for searchController: UISearchController) {
        
        let searchString = searchController.searchBar.text!
        
        
        if searchString.isEmpty {
            filteredobjects.removeAll()
        }else{
            
            let filterObjects = (cat?.category.map({
                $0.skill.filter({$0.vName.lowercased().contains(searchString.lowercased())})
            }).filter({ $0.count > 0 }))!

          
            filteredobjects = filterObjects
         
            print("filter data count is in search before \(filteredobjects.count)")
        //    print("filter data is \(filteredobjects)")
           
        }
        
        if searchController.isActive {
            isSearching = true
        }else{
            isSearching = false
        }
        tableView.reloadData()
    }
    
    private func parseJSON(){
        guard let path = Bundle.main.path(forResource: "category",ofType: "json")else{
            return
        }
        
        let url = URL(fileURLWithPath: path)
        
        do {
            let jsonData = try Data(contentsOf: url)
            
            cat = try JSONDecoder().decode(Welcome.self,from: jsonData)
            
            if let result = cat {
                products = result.category
                //    print(result)
            }
        } catch  {
            print("Error is \(error)")
        }
    }
    
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if isSearching && searchController.searchBar.text != "" {
   
            return filteredobjects.count   //final
           
        }else{
            return products.count
        }
       
    }
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        
        if isSearching && searchController.searchBar.text != ""{
            
            return filteredobjects[section].count  //final
           
        }else{
            return cat!.category[section].skill.count
        }
    }
    
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
        
        if isSearching && searchController.searchBar.text != "" {
            let text = filteredobjects[indexPath.section][indexPath.row]
            cell.textLabel?.text = text.vName
        }else{
        let text = cat?.category[indexPath.section].skill[indexPath.row].vName
            cell.textLabel?.text = text
        }
        
        if selectedIngredients.contains(indexPath){
            cell.accessoryType = .checkmark
        }else{
            cell.accessoryType = .none
        }
    cell.selectionStyle = .none
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
        
        
        let titleText = cat?.category[section].vName
        return titleText
    }
    override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        
        if self.selectedIngredients.contains(indexPath){
            self.selectedIngredients.remove(indexPath)
        }else{
            self.selectedIngredients.insert(indexPath)
            
        }
        self.tableView.reloadData()
 
    }
    
    
}

看起来像this

请忽略其他无关代码 帮助我或任何提示,帮助我或任何提示, 非常感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。