ios - UISearchController 使用 Swift 2 在另一个数组中搜索数组项

标签 ios swift uitableview uisearchcontroller

我有带有 searchBar 的 tableview Controller ,我想在数组中搜索项目 在我的示例中,我想在 leagues 数组中搜索 teamsArr 项目

现在我的代码在 Legue Name 中搜索正常,而不是在 Teams name 中搜索

我的代码:

import UIKit

struct Legue {
var name:String
var num:Int
var teamsArr:[String]
}

class Table1ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating {

@IBOutlet var myTable: UITableView!

let cellID="MyCellID"
var Legues:[Legue]=[]
var searchController=UISearchController(searchResultsController: nil)
var filterLegues:[Legue]=[]





func filterContentForSearch(searchText:String,scope:String="All2")
{
    filterLegues=Legues.filter{leg in
        return leg.name.containsString(searchText) **\\ Here search only in Legue name ,but i want to search in teams name**
     }


    myTable.reloadData()
}
override func viewDidLoad() {
    super.viewDidLoad()

    myTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellID)

    myTable.delegate=self
    myTable.dataSource=self
    Legues=self.loadData()

    searchController.searchResultsUpdater=self

    searchController.dimsBackgroundDuringPresentation=false
    definesPresentationContext=true
    myTable.tableHeaderView=searchController.searchBar
    searchController.hidesNavigationBarDuringPresentation=false


}




func loadData()->[Legue]
{
    let arrlegue=[Legue(name:"Spain",num: 3,teamsArr: ["Barcelona","Atletico Madrid","Real Madrid"]),Legue(name:"Saudi",num: 4,teamsArr: ["Ahli","Hilal","AlNaser","AlEtihad"]),Legue(name:"England",num: 2,teamsArr:["Lestercity","Man City"]),Legue(name:"Italy",num: 5,teamsArr: ["Juventus","Napoli","AS Roma","Internazionale","AC Milano"])]

    return arrlegue
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

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

    if searchController.active && searchController.searchBar.text != ""
   {
    let x=filterLegues[section].teamsArr.count
    return x
    }

    let x=Legues[section].teamsArr.count
    return x
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if searchController.active && searchController.searchBar.text != ""
    {
    return filterLegues.count
    }
    else
    {
        return Legues.count
    }
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if searchController.active && searchController.searchBar.text != ""
    {
    return filterLegues[section].name
    }
    else
    {
        return Legues[section].name
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell=myTable.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
    if searchController.active && searchController.searchBar.text != ""
    {
  cell.textLabel?.text="\(filterLegues[indexPath.section].teamsArr[indexPath.row])"
        cell.imageView?.image=UIImage(named: filterLegues[indexPath.section].name)
    }
    else
    {
    cell.textLabel?.text="\(Legues[indexPath.section].teamsArr[indexPath.row])"
    cell.imageView?.image=UIImage(named: Legues[indexPath.section].name)
    }
        return cell
}

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    if sourceIndexPath != destinationIndexPath
    {
    let itemToMove=Legues[sourceIndexPath.section]
    Legues.removeAtIndex(sourceIndexPath.section)
    Legues.insert(itemToMove, atIndex: destinationIndexPath.section)

    myTable.reloadData()
    }
    }

@IBAction func editButton(sender: UIBarButtonItem) {

  myTable.editing=true
}

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filterContentForSearch(searchController.searchBar.text!)


}

最佳答案

我的代码就是这样,而且它有效:

//
//  InterestsTableViewController.swift
//  OyventIOSApp
//
//  Created by Mehmet Sen on 9/9/16.
//  Copyright © 2016 Mehmet Sen. All rights reserved.
//

import UIKit

class InterestsTableViewController: UITableViewController {

    let api: InterestAPI = InterestAPI()
    var pkUserID : Double!
    var ismyprofile : Bool! = false
    var sections: [SectionData] = []
    let searchController = UISearchController(searchResultsController: nil)
    var filteredSections: [SectionData] = []


    func filterContentForSearchText(searchText: String, scope: String = "ALL") {

        filteredSections = sections.map{ (section) -> SectionData in
            let interests = section.interests.filter{$0.name!.localizedCaseInsensitiveContainsString(searchText.lowercaseString)}
            return SectionData(title: section.title, interests: interests)}.filter{$0.numberOfItems > 0
        }
        tableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        pkUserID =  NSNumberFormatter().numberFromString(NSUserDefaults.standardUserDefaults().stringForKey("blabla_id")!)?.doubleValue

        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar

        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        api.fetchUserInterestList(pkUserID) { (results: NSDictionary) in

            var resultsArr: [Interest] = results["results"] as! [Interest]
            dispatch_async(dispatch_get_main_queue(), {

                resultsArr = Interest.interestsWithJSON(resultsArr)
                for i in 0 ..< resultsArr.count {

                    var exists: Bool = false

                    for j in 0 ..< self.sections.count {
                        if self.sections[j].title == resultsArr[i].scope{
                            exists = true
                            self.sections[j].interests.append(resultsArr[i])
                        }
                    }

                    if(exists == false){
                        let sectionData = SectionData(title: resultsArr[i].scope!, interest: resultsArr[i])
                        self.sections.append(sectionData)
                    }


                }

                self.tableView!.reloadData()
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            })
        }

    }


    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return searchController.active && searchController.searchBar.text != "" ? filteredSections.count : sections.count
    }

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

        return searchController.active && searchController.searchBar.text != "" ? filteredSections[section].numberOfItems  : sections[section].numberOfItems
    }


    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section].title
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.clearColor()

        let interest:Interest
        interest = searchController.active && searchController.searchBar.text != "" ? filteredSections[indexPath.section][indexPath.row] : sections[indexPath.section][indexPath.row]

        // insert the special characters using edit > emoji on the menu
        cell.textLabel?.textColor = UIColor.whiteColor()
        cell.textLabel!.text = interest.checked  ? "✅ " + interest.name! : interest.name!

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        //get the interest object
        let interest:Interest = sections[indexPath.section][indexPath.row]

        if(self.ismyprofile != false){
            interest.toggleCheck()

            //add user interest
            if(interest.checked){

                api.addUserInterest(pkUserID, interestid: interest.interestid!, completionHandler: { (result: NSDictionary) in

                    let resultValue: Bool = result["success"] as! Bool!
                    if(resultValue){
                        //self.interests[indexPath.row] = interest
                        self.sections[indexPath.section][indexPath.row] = interest
                    }else{
                        interest.toggleCheck() //reverse back
                    }
                })
            }else{//delete user interest

                api.deleteUserInterest(pkUserID, interestid: interest.interestid!, completionHandler: { (result: NSDictionary) in

                    let resultValue: Bool = result["success"] as! Bool!
                    if(resultValue){
                        //self.interests[indexPath.row] = interest
                        self.sections[indexPath.section][indexPath.row] = interest
                    }else{
                        interest.toggleCheck() //reverse back
                    }
                })
            }
        }

        tableView.reloadData()
    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


class SectionData {
    var title: String
    var interests : [Interest] = []

    init(title: String, interest: Interest) {
        self.title = title
        self.interests.append(interest)
    }

    init(title: String, interests: [Interest]) {
        self.title = title
        self.interests = interests
    }

    var numberOfItems: Int {
        return interests.count
    }

    subscript(index: Int) -> Interest {

        get {
            return interests[index]
        }
        set(newValue) {
            interests[index] = newValue
        }

    }
}


extension InterestsTableViewController: UISearchResultsUpdating {
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        filterContentForSearchText(searchController.searchBar.text!)
    }
}


//
//  Interest.swift
//  OyventIOSApp
//
//  Created by Mehmet Sen on 8/11/16.
//  Copyright © 2016 Mehmet Sen. All rights reserved.
//

import Foundation

class Interest {

    var interestid: Int?
    var name: String?
    var userid: Double?
    var scope: String?
    var checked: Bool = false

    init(interestid: Int, name: String, userid: Double, scope: String, checked: Bool){

        self.interestid = interestid
        self.name = name
        self.userid = userid
        self.scope = scope
        self.checked = checked
    }

    class func interestsWithJSON(allResults: NSArray) -> [Interest] {

        var interests = [Interest]()

        if allResults.count > 0 {

            for result in allResults {

                let interestid = result["pkinterestid"] as? Int
                let name = result["name"] as? String
                let userid = result["fkuserid"] as? Double
                let scope = result["scope"] as? String
                let checked: Bool = userid > 0 ? true : false

                //print("interestid: \(interestid!) name: \(name!) userid: \(userid!)  checked: \(checked)")

                let newInterest = Interest(interestid: interestid!, name: name!, userid: userid!, scope: scope!, checked: checked)
                interests.append(newInterest)
            }
        }

        return interests;
    }

    internal func toggleCheck(){
        self.checked =  !self.checked
    }
}

关于ios - UISearchController 使用 Swift 2 在另一个数组中搜索数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36971135/

相关文章:

ios - TableView 在数据加载之前出现

iphone - 一个文件中的两个 UITableView

ios - 如何在 View 出现后立即启动核心动画?

ios - OpenCV 2.4.4 和 Tesseract 3.02.02 不要链接在一起

ios - 为正文文本样式启用 iOS 系统字体中的排版功能

arrays - Swift 数组行为

ios - Mapbox Geocoding API iOS 是否对大片水域(海洋)进行反向地理编码?

objective-c - 使单元格在 TableView 中出现和消失

iphone - geocodeAddressString无法识别的选择器错误处理结果

ios - 无法为 IOS 构建 react-native-xmpp 库