如何使用Multiselection在过滤的uitableview swift 3中获取原始索引路径

时间:2017-08-30 12:53:15

标签: ios swift3

有一个需要搜索它的uitableView。当我单击一个单元格时,它会在该单元格中显示一个指示符(它是多选表)。到目前为止,每件事都很有效。但是,当我搜索某些内容并返回到原始表时,返回的索引不是原始索引,它表示错误的单元格。我在stackoverflow中看到了很多问题,但是他们使用的是故事板或者它们是在Objective中-c我不明白。

  

如何在搜索表中获得原始indexPath(多选)?

导入UIKit

代码..............

class SearchViewController: UIViewController, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate
{

    var marrCountryList: NSMutableArray = NSMutableArray()
    var marrFilteredCountryList: NSMutableArray = NSMutableArray()
    var selectedArray : NSMutableArray = NSMutableArray()


    @IBOutlet var tblCountry: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        marrCountryList = ["FRK","USA", "Bahamas", "Brazil", "Canada", "Republic of China", "Cuba", "Egypt", "Fiji", "France", "Germany", "Iceland", "India", "Indonesia", "Jamaica", "Kenya", "Madagascar", "Mexico", "Nepal", "Oman", "Pakistan", "Poland", "Singapore", "Somalia", "Switzerland", "Turkey", "UAE", "Vatican City"]
        self.tblCountry.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            return self.marrFilteredCountryList.count
        }
        else
        {
            return self.marrCountryList.count
        }
    }

    @IBAction func btnnewSubmit(_ sender: Any) {

        let objReg=self.storyboard?.instantiateViewController(withIdentifier: "ViewTableViewController") as! ViewTableViewController
        print("submite button tapped...")
        objReg.abc = selectedArray
        print(selectedArray)
        self.navigationController?.pushViewController(objReg, animated: true)
    }

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
    {
        let cellCountry = self.tblCountry.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        var countryName : String!
        if tableView == self.searchDisplayController!.searchResultsTableView
        {
            countryName = marrFilteredCountryList[indexPath.row] as! String
            tblCountry.reloadData()
        }
        else
        {
            countryName = marrCountryList[indexPath.row] as! String

        }

        if selectedArray.contains(self.marrCountryList.object(at: indexPath.row) as! String)
        {
            let checkImage = UIImage(named:"check")
            let checkers = UIImageView(image: checkImage)
            cellCountry.accessoryView = checkers
        }
        else
        {
            let uncheckImage = UIImage(named:"uncheck")
            let unchecker = UIImageView(image: uncheckImage)
            cellCountry.accessoryView = unchecker
        }

        cellCountry.textLabel?.text = countryName

        return cellCountry
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        if let cell = tableView.cellForRow(at: indexPath)
        {

            if selectedArray.contains(self.marrCountryList.object(at: indexPath.row) as! String)
            {
                let uncheckImage = UIImage(named:"uncheck")
                let unchecker = UIImageView(image: uncheckImage)
                cell.accessoryView = unchecker
                selectedArray.remove(self.marrCountryList.object(at: indexPath.row) as! String)
                tblCountry.reloadData()

            }
            else
            {
                let checkImage = UIImage(named:"check")
                let checkers = UIImageView(image: checkImage)
                cell.accessoryView = checkers
                selectedArray.add(self.marrCountryList.object(at: indexPath.row) as! String)
                tblCountry.reloadData()


            }


        }

        var object :AnyObject?
        if(self.searchDisplayController?.isActive)! {
            object = marrFilteredCountryList[indexPath.row] as AnyObject
        }
        else {
            object = self.marrCountryList[indexPath.row] as AnyObject
        }

    }

    func filterTableViewForEnterText(_ searchText: String) {
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)

        let array = (self.marrCountryList as NSArray).filtered(using: searchPredicate)
        self.marrFilteredCountryList = array as! [String] as! NSMutableArray

    }

    func searchDisplayController(_ controller: UISearchDisplayController, shouldReloadTableForSearch searchString: String?) -> Bool
    {
        self.filterTableViewForEnterText(searchString!)
        return true
    }

    func searchDisplayController(_ controller: UISearchDisplayController,
        shouldReloadTableForSearchScope searchOption: Int) -> Bool
    {

            self.filterTableViewForEnterText(self.searchDisplayController!.searchBar.text!)
            return true
    }

}

0 个答案:

没有答案