lundi 10 août 2020

Searching Functionality with Checkbox value saved in contact list Swift 5

What code should i add to save checkbox values with names in contact list app? I want to search names with saved checkbox that i have added. This code is running search functionality but not able to store checkbox values. Checkbox values storing on wrong indexpath. Thankyou in advance! import UIKit class ViewController: UIViewController {

  var username: [String] = ["Aman","Ajay","Ankit","Ashu","Bhanu","Chetan","Doctor","Amann"]
    let search = UISearchController(searchResultsController: nil)
    
    var filteredNames: [String] = []
    var nameArray = [String]()
   
    var text: String?
    var indexOfSelectedItem: Int?
    
    var selectedCells:[String] = []
    
    var checkArray : NSMutableArray = NSMutableArray()
    var usrName : NSMutableArray = NSMutableArray()
   
    @IBOutlet weak var tblView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad() 
        self.navigationItem.title = "Contacts"
        self.navigationController?.navigationBar.prefersLargeTitles = true
        
        search.delegate = self
        search.searchBar.delegate = self
        search.searchResultsUpdater = self
        self.navigationItem.searchController = search
        search.obscuresBackgroundDuringPresentation = false
        
        
        self.usrName.removeAllObjects()
        for items in username {
            print (items)
            let model = RNCheckedModel()
            model.user_name = items
            model.isCheck = false
            self.usrName.add(model)
       }
    }

}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return username.count
        
        if (search.isActive){
            return filteredNames.count
        }
        else{
            filteredNames = username
            return username.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tblView.dequeueReusableCell(withIdentifier: "CELL_IDENTIFIER", for: indexPath) as! contactsTVC
           if(search.isActive){
                 cell.lblName.text = filteredNames[indexPath.row]
                 return cell
             }
             else {
            let model = self.usrName[indexPath.row] as! RNCheckedModel
            cell.lblName.text = model.user_name
                 return cell
            }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if self.selectedCells.contains(username[indexPath.row]) {
        self.selectedCells.remove(at: self.selectedCells.firstIndex(of: username[indexPath.row])!)
        } else {
            self.selectedCells.append(username[indexPath.row])
        }
        tableView.reloadData()
    }
}

extension ViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        self.tblView.reloadData()
    }
    
    func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let newString = (searchBar.text! as NSString).replacingCharacters(in: range, with: text)
        filteredNames = username.filter { $0.lowercased().contains(newString.lowercased()) }
        
        if newString == ""{
            filteredNames = username
        }
        self.tblView.reloadData()
        return true
    }
    
}



Aucun commentaire:

Enregistrer un commentaire