搜索时如何显示正确的数据

时间:2017-04-30 12:15:55

标签: ios swift xcode search master-detail

我有一个主细节应用程序,为每个单元格视图显示不同的数据, 我的问题是,当我用搜索控制器搜索和过滤单元格时,单元格视图控制器的detailView上的数据会变得混乱。

搜索代码

   var searchController : UISearchController!

    struct User {
        var name: String
        var streetName: String
        var image: UIImage?
    }

    var resultsController = UITableViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableViewController.dataSource = self

        //SearchBar Settings :
        definesPresentationContext = true
        self.resultsController.tableView.dataSource = self
        self.resultsController.tableView.delegate = self
        self.searchController = UISearchController(searchResultsController: self.resultsController)
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.searchResultsUpdater = self
        self.searchController.dimsBackgroundDuringPresentation = true
        self.searchController.searchBar.sizeToFit()
        self.searchController.searchBar.barTintColor = UIColor.black
        self.searchController.searchBar.endEditing(true)
        self.searchController.searchBar.placeholder = "חפש ברים"
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.searchBar.delegate = self


        //MARK : Fixes SearchBar Bugs
        self.resultsController.edgesForExtendedLayout = .all
        self.searchController.edgesForExtendedLayout = .all
        self.resultsController.extendedLayoutIncludesOpaqueBars = true
        // End of Bug Fixes.

        ////// MARK: END Of Search //////

        displayWalkthroughs()

        allUsers = createUsers(names: names, streets: streets, images: images)

        filteredUsers = allUsers

        }

override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.searchController.searchBar.resignFirstResponder()
    self.searchController.searchBar.endEditing(true)
}

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

//Search.
var allUsers: [User]!
var filteredUsers: [User]!

func createUsers(names: [String], streets: [String], images: [UIImage?]) -> [User] {
    var users = [User]()
    guard names.count == streets.count && names.count == images.count else { return users }
    for (index, name) in names.enumerated() {
        let user = User(name: name, streetName: streets[index], image: images[index])
        users.append(user)
    }
    return users
}

//MARK : Search !
func  updateSearchResults(for searchController:UISearchController) {
    if let searchText = searchController.searchBar.text?.lowercased() {
        if searchText.characters.count == 0 {
            filteredUsers = allUsers
        }
        else {
            filteredUsers = allUsers.filter {
                return $0.name.lowercased().contains(searchText) ||
                    $0.streetName.lowercased().contains(searchText)
                } 
        }
    }
    self.resultsController.tableView.reloadData()
}

视图控制器

class TemplateTestViewController: UIViewController,MKMapViewDelegate {

    @IBOutlet var HeaderProfileName: UILabel!
    @IBOutlet var HeaderprofileImage: UIImageView!
    @IBOutlet var StreetProfileLabel: UILabel!
    @IBOutlet var ProfilePhoneNumberLabel: UIButton!
    @IBOutlet var WorkTimeLabel: UILabel!
    @IBOutlet var AgeLabel: UILabel!
    @IBOutlet weak var OnlineMenuButton: UIButton!
    @IBOutlet weak var FirstOpeningHours: UILabel!
    @IBOutlet weak var SecondOpeningHours: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()


        //Sets Up The BIG Name Template :
        HeaderProfileName.text = barName
        //Sets Up Street Text :
        StreetProfileLabel.text = streetName
        //Sets Up The Navigation Bar Name :
        navigationItem.title = barName
        //Set Ups the Image Template :
        HeaderprofileImage.image = UIImage(named: ProfileImages[MyIndex])
        //Sets Up Bar Or Club Text :
        BarOrClubLabel.text = ProfileBarOrClub[MyIndex]
        //Sets Up Age Text :
        AgeLabel.text = AgeText[MyIndex]
        //Sets Up Phone Number Text :
        ProfilePhoneNumberLabel.setTitle(ProfilePhoneNumbers[MyIndex], for: .normal)
        //Sets Up OpeningHours :
        FirstOpeningHours.text = firstOpeningHours[MyIndex]
        //Sets up Second Opening Hours :
        SecondOpeningHours.text = secondOpeningHours[MyIndex]

        OnlineMenuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.right

数据

var MyIndex = 0

//Template Data:
var ProfileImages = ["",""]
var ProfileBarOrClub = ["",""]
var ProfilePhoneNumbers = ["",""]

索引

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell

        let user:User!

        if tableView == self.tableView {
            user = allUsers[indexPath.row]
        } else {
            user = filteredUsers[indexPath.row]
        }
        cell.photo.image = user.image
        cell.name.text = user.name
        cell.streetName.text = user.streetName

        return cell
    }


   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){


        let object = self.storyboard?.instantiateViewController(withIdentifier: "TemplateTestViewController") as! TemplateTestViewController
        let user:User!
        if tableView == self.tableView {
            user = allUsers[indexPath.row]
        } else {
            user = filteredUsers[indexPath.row]
        }

      print("username : \(user.name)")
        print("streetName : \(user.streetName)")

        object.barImage = user.image!
        object.barName = user.name
        object.streetName = user.streetName



        self.navigationController?.pushViewController(object, animated: true)



    MyIndex = indexPath.row
}

0 个答案:

没有答案