想要在使用搜索栏搜索时在表视图中显示主要类别的子类别数据

时间:2019-07-12 14:15:22

标签: ios swift parsing tableview searchbar

我有一个应用程序,其中在我的HomeVc中,我使用两个表视图,一个用于显示collectionview单元的数据,另一个用于显示来自同一API的搜索结果。现在的问题是,如果未选择api中的搜索参数,则应显示整个数据,包括类别和子类别;如果选择了搜索参数,则应选择特定的数据,包括类别和子类别。例如:“食品”是主要类别,“水果”,“蔬菜”等是子类别。现在,当我键入食物时,应显示与食物相关的所有数据,即水果,蔬菜等,如果我键入水果,则应仅显示水果。我已经尝试过但没有得到。我希望当我搜索食物时,食物,水果,蔬菜等应显示。当我仅搜索“水果”时,应该显示水果,而当什么都没有时,则应该显示所有数据。我尝试了所有可能的方法,但是没有运气。谁能帮助我。

在我的代码中。我已经实现了所有tableview数据源和委托方法,我认为在cellforrow方法中,出现了问题,而在API解析功能中,有问题了

我的json数据链接:json data link

我的homevc的

屏幕快照,其中显示了作为tableview标头seciton列出的类别以及在collectionview单元格中列出的子类别,我也想在点击子类别时导航到每个词都包含在每个子类别下的屏幕: homevc image

选择类别和子类别时得到的结果屏幕截图

[screenshot1][1]
[screenshot2][2]

screenshot of my api with all data showing, category data showing and subcategory data showing

[screen1][3]
[screen2][4]
[screen3][5]

I am using Swifty json software to create model class with object mapper

link to my model class is: [link to my model class][6]

my homevc code :



class HomeViewController: UIViewController{

        var cardCategories = [ModelGetCardsResult]()
        var searchedData = [ModelGetCardsResult]()

    //    var searchListData = [ModelSearchResult]()

        var isSearch = Bool()      

        //MARK: Get_cards API Parsing

        func WS_GetCards() {

            if ApiUtillity.sharedInstance.isReachable() {

                let parameters = [
                    "cardname": "\(self.popup_searchBar.text ?? "")"

                    ] as [String : Any]

                ApiUtillity.sharedInstance.StartProgress(view: self.view)

                APIClient<ModelBaseGetCards>().API_POST(Url: WS_PostCardsAPI, Params: parameters as [String : AnyObject], Authentication: true, Progress: true, Alert: true, Offline: false, SuperVC: self, completionSuccess: { (modelResponse) in

                    if self.popup_searchBar.text?.count != 0 {
    //                    self.searchedData.removeAll()
                        self.searchedData.removeAll()
                    }
                    else {
                        self.cardCategories.removeAll()
                    }

                    if(modelResponse.success == true) {

                        ApiUtillity.sharedInstance.StopProgress(view: self.view)

                            if let tempCardCategories = modelResponse.getCardsResult, tempCardCategories.count != 0 {
                                for card in tempCardCategories {
                                    if self.popup_searchBar.text?.count != 0 {
                                        self.searchedData.append(card)
                                    }
                                    else {
                                        self.cardCategories.append(card)
                                    }
                                }
                        }else {

                        }

                    }else {
                        self.showToast(message: modelResponse.message!)
                         ApiUtillity.sharedInstance.StopProgress(view: self.view)
                    }

                    if self.popup_searchBar.text?.count != 0 {
                        if self.searchedData.count == 0{
                            self.setNoDataMessage(message: modelResponse.message)
                        }
                    }
                    else {
                        if self.cardCategories.count == 0{
                            self.setNoDataMessage(message: modelResponse.message)
                        }
                    }
                    ApiUtillity.sharedInstance.StopProgress(view: self.view)
    //                self.popup_searchTblView.reloadData()
                    self.reloadTableview()

                }) { (failed) in
                    self.setNoDataMessage(message: failed.localizedDescription)
                    ApiUtillity.sharedInstance.StopProgress(view: self.view)
                    self.showToast(message: failed.localizedDescription)

    //                self.popup_searchTblView.reloadData()
                    self.reloadTableview()
                }
            }else {
                self.showToast(message: "No internet connection...")
    //            self.popup_searchTblView.reloadData()
                self.reloadTableview()
            }
        }
    }

    extension HomeViewController: UITableViewDataSource, UITableViewDelegate{

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

            var noOfSection = 1
            switch tableView {
            case popup_searchTblView:
                noOfSection = 1
            case cardsTableView:
                noOfSection = cardCategories.count
            default:
                break
            }
            return noOfSection
        }

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

            var noOfRow = 1
            switch tableView {
            case popup_searchTblView:
                if popup_searchBar.text?.count == 0 {
                   tableView.setEmptyView(message: "Try searching card/words data..", messageImage: UIImage(named: "magnifier")!)
                }else {
                    tableView.restore()
                }
                noOfRow = searchedData.count

                if popup_searchBar.text?.count != 0 {
                   noOfRow = searchedData.count
                } 

            case cardsTableView:
                noOfRow = 1
            default:
                break
            }
            return noOfRow
        }

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

            let tableCell = UITableViewCell()

            if tableView == self.popup_searchTblView {
                let cell = tableView.dequeueReusableCell(withIdentifier: "SearchDataCell", for: indexPath) as! SearchDataCell
                if popup_searchBar.text?.count != 0 {

                    if let cardName = self.searchedData[indexPath.row].categoryName, cardName.count != 0 {

                        cell.searchedWords.text = cardName
                    }
                }
                return cell

            } else if tableView == self.cardsTableView {
                let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryRow", for: indexPath) as! CategoryRow
                cell.delegate = self as CustomDelegate
                cell.cardsCollView.contentOffset = .zero

                 let arrName = cardCategories[indexPath.section]

                switch arrName.categoryName ?? "" {

                case "VERBS":
                    cell.items = cardCategories[indexPath.section].verbs ?? []
                    break
                case "ADJECTIVES":
                    cell.items = cardCategories[indexPath.section].adjectives ?? []
                    break
                case "SPORTS":
                    cell.items = cardCategories[indexPath.section].sports ?? []
                    break
                case "COMMON PHRASES":
                    cell.items = cardCategories[indexPath.section].commonPhrases ?? []
                    break
                case "SCHEDULE":
                    cell.items = cardCategories[indexPath.section].schedule ?? []
                    break
                case "FOOD":
                    cell.items = cardCategories[indexPath.section].food ?? []
                    break
                case "DATING":
                    cell.items = cardCategories[indexPath.section].dating ?? []
                    break
                case "AT SCHOOL":
                    cell.items = cardCategories[indexPath.section].atSchool ?? []
                    break
                case "TRAVEL":
                    cell.items = cardCategories[indexPath.section].travel ?? []
                    break
                case "BASICS":
                    cell.items = cardCategories[indexPath.section].basics ?? []
                    break
                case "OUTSIDE":
                    cell.items = cardCategories[indexPath.section].outside ?? []
                    break
                case "PEOPLE":
                    cell.items = cardCategories[indexPath.section].people ?? []
                    break

                default:
                    break
            }
                return cell
        }
            return tableCell
     }

[1]: https://i.stack.imgur.com/pIG1T.png
  [2]: https://i.stack.imgur.com/w4DUw.png
  [3]: https://i.stack.imgur.com/uHBDs.png
  [4]: https://i.stack.imgur.com/Gn9zK.png
  [5]: https://i.stack.imgur.com/oNnHH.png
      https://drive.google.com/file/d/1CNsfzZbwEuKzWxepPr5SON93rW3KDNtz/view?usp=sharing

     [1]: https://drive.google.com/file/d/1suPhRMtArMXD0B92zJyearBQw_W247g5/view?usp=sharing

0 个答案:

没有答案