无法根据所选数组更新行数?

时间:2019-05-04 08:38:42

标签: ios swift uitableview

首先,对不起下面给出的冗长代码。 这是因为我无法弄清我想念的是什么或错了

我想根据单击的按钮在表视图中显示数组..并且当用户搜索..它应该过滤表视图时...当我在行数中给出一个静态的3位数值时,一切工作正常。表格视图只是空白。

class RegisterViewController: UIViewController {

    @IBOutlet var txtUser: UITextField!
    @IBOutlet var txtPhone: UITextField!
    @IBOutlet var txtEmail: UITextField!
    @IBOutlet var SelectionView: UIView!
    @IBOutlet var pickerTableView: UITableView!
    @IBOutlet var btnDawat: UIButton!
    @IBOutlet var btnCountry: UIButton!
    @IBOutlet var btnNationality: UIButton!

    @IBOutlet var searchItems: UISearchBar!
    let manager = APIManager()

    var isSearching = Bool()

    enum options {
        case dawat
        case country
        case nationality
    }

    var lastSelection:options?

    override func viewDidLoad() {
        super.viewDidLoad()

        isSearching = false

        manager.parsingGet(url: BaseURL.countries) { (JSON, Bool) in


            let dict = JSON.dictionaryObject
            let countries = dict!["data"] as! NSArray
            signUser.countries = countries as! [String]
            let nations = dict!["data"] as! NSArray
            signUser.nationalities  = nations as! [String]               

        }

            pickerTableView.reloadData()

    }


    @IBAction func DawatTitleSelectionAction(_ sender: UIButton) {

        lastSelection = options.dawat
        pickerTableView.reloadData()
        SelectionView.center = self.view.center
        self.view.addSubview(SelectionView)

    }

    @IBAction func CountrySelectionButton(_ sender: UIButton) {

        lastSelection = options.country
        pickerTableView.reloadData()
        SelectionView.center = self.view.center
        self.view.addSubview(SelectionView)

    }

    @IBAction func NationalitySelectionAction(_ sender: UIButton){

        lastSelection = options.nationality
        pickerTableView.reloadData()
        SelectionView.center = self.view.center
        self.view.addSubview(SelectionView)            
    }

    @IBAction func RegisterButtonPressed(_ sender: UIButton) {


    //    Signup.email = self.txtEmail.text!
    //    Signup.phone = self.txtPhone.text!

    }

    @IBAction func pickerSelectButton(_ sender: UIButton) {            

        SelectionView.removeFromSuperview()            

    }        
}

// tableview和搜索功能

    extension RegisterViewController : UITableViewDataSource,UITableViewDelegate  {


    func numberOfSections(in tableView: UITableView) -> Int {

        return 1

    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

          print(signUser.tableData.count)

        return signUser.tableData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PickerTableViewCell

        if isSearching == false{
           // print("presenting raw data")

            switch lastSelection!{

            case .country:
              //  print("raw countries list")
                signUser.unfilteredArray = signUser.countries
            case .nationality:
               // print("raw nationalities list")
                signUser.unfilteredArray = signUser.nationalities
            case .dawat:
                signUser.unfilteredArray = signUser.dawatTitles

            }

           signUser.tableData = signUser.unfilteredArray

         }

        else {
           // print("presenting filtered data")


            switch lastSelection!{

            case .country:
               // print("filtered countries")
                signUser.countries = signUser.filteredArray
                signUser.tableData = signUser.filteredArray
            case .nationality:
               // print("filtered nationalities")
                 signUser.nationalities = signUser.filteredArray
                signUser.tableData = signUser.filteredArray
            case .dawat:
                 signUser.dawatTitles = signUser.filteredArray
                signUser.tableData = signUser.filteredArray
            }

        }
            cell.lblTitle.text = signUser.tableData[indexPath.row]
            return cell            
    }


extension RegisterViewController : UISearchBarDelegate {


    //searchbar

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        signUser.filteredArray.removeAll(keepingCapacity: false)

        if let searchText = searchItems.text, !searchText.isEmpty {

            isSearching = true

            let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)

            signUser.filteredArray = (signUser.unfilteredArray as NSArray).filtered(using: searchPredicate) as! [String]


        }

        else {

            signUser.tableData = signUser.unfilteredArray
            isSearching = false

        }            

        pickerTableView.reloadData()            
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {

        isSearching = false

    }
}

2 个答案:

答案 0 :(得分:2)

更改按钮操作方法以更改signUser.tableData

@IBAction func DawatTitleSelectionAction(_ sender: UIButton) {

    lastSelection = options.dawat
    signUser.filteredArray = signUser.dawatTitles
    signUser.unfilteredArray = signUser.dawatTitles
    signUser.tableData = isSearching ? signUser.filteredArray : signUser.unfilteredArray
    pickerTableView.reloadData()
    SelectionView.center = self.view.center
    self.view.addSubview(SelectionView)

}

@IBAction func CountrySelectionButton(_ sender: UIButton) {

    lastSelection = options.country
    signUser.filteredArray = signUser.countries
    signUser.unfilteredArray = signUser.countries
    signUser.tableData = isSearching ? signUser.filteredArray : signUser.unfilteredArray
    pickerTableView.reloadData()
    SelectionView.center = self.view.center
    self.view.addSubview(SelectionView)

}

@IBAction func NationalitySelectionAction(_ sender: UIButton){

    lastSelection = options.nationality
    signUser.filteredArray = signUser.nationalities
    signUser.unfilteredArray = signUser.nationalities
    signUser.tableData = isSearching ? signUser.filteredArray : signUser.unfilteredArray
    pickerTableView.reloadData()
    SelectionView.center = self.view.center
    self.view.addSubview(SelectionView)
}

更新tableView委托方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(signUser.tableData.count)
    return signUser.tableData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PickerTableViewCell
    cell.lblTitle.text = signUser.tableData[indexPath.row]
    return cell
}

更新搜索栏委托方法

extension RegisterViewController : UISearchBarDelegate {
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            if let searchText = searchItems.text, !searchText.isEmpty {
                isSearching = true
                let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)
                signUser.filteredArray = (signUser.unfilteredArray as NSArray).filtered(using: searchPredicate) as! [String]
                signUser.tableData = signUser.filteredArray
            }
            else {
                signUser.tableData = signUser.unfilteredArray
                isSearching = false
            }
            pickerTableView.reloadData()
        }
        func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
            isSearching = false
        }
}

答案 1 :(得分:1)

  • 使用两个数组,例如未过滤的数组和已过滤的数组

    var tableData = [Model]()
    var filteredArray = [Model]()
    

    从不tableData替换为过滤后的内容。

  • searchBar:textDidChange中,按lastSelection过滤数据,填充filteredArray并设置isSearching

  • 在数据源中,方法根据isSearching的状态显示数据(简化)

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return isSearching ? filteredArray.count : tableData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PickerTableViewCell
        let item : Model
        if isSearching {
           item = filteredArray[indexPath.row]
        } else {
           item = tableData[indexPath.row]
        }
        // assign the values of item to the UI
        return cell
    }