搜索TableView时搜索结果被截断

时间:2016-12-06 01:20:40

标签: ios swift3 xcode8 uisearchcontroller

我的搜索工作正常,但显示无法按预期工作。当我执行搜索时,最后的搜索结果略微被切断,显示部分全部为灰色。我希望它与行保持相同的背景颜色..任何帮助都将不胜感激。

cutoff cutoff2

编辑:

import UIKit
import Foundation

// For search
extension MasterViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }
}

class MasterViewController: UITableViewController {

    let searchController = UISearchController(searchResultsController: nil)

    var filteredFighters = [Fighter]()

    var detailViewController: DetailViewController? = nil
    var objects = [Any]()

    // Creates array of fighters
    var fighterArray = [Fighter]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.navigationItem.leftBarButtonItem = self.editButtonItem

        if let split = self.splitViewController {
            let controllers = split.viewControllers
            self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
        }

        // Access all the data from JSON
        let JR = JSONReceiver()
        fighterArray = JR.populateFighterJSONArray()


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

        navigationItem.title = "UFC Champions"

        self.navigationController?.navigationBar.barTintColor = UIColor.black
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [ NSForegroundColorAttributeName : UIColor.white, NSFontAttributeName: UIFont(name: "Arial", size: 26)!]

    }

    // FOr search
    func filterContentForSearchText(searchText: String, scope: String = "All") {
        filteredFighters = fighterArray.filter { fighter in
            return fighter.name.lowercased().contains(searchText.lowercased())
        }

        tableView.reloadData()
    }
    override func viewWillAppear(_ animated: Bool) {
        self.clearsSelectionOnViewWillAppear = self.splitViewController!.isCollapsed
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

        /*   DONT BELIEVE THIS IS NEEDED - DO NOT SEE IN PROFESSORS CODE
     func insertNewObject(_ sender: Any) {
        objects.insert(NSDate(), at: 0)
        let indexPath = IndexPath(row: 0, section: 0)
        self.tableView.insertRows(at: [indexPath], with: .automatic)
    } */

    // MARK: - Segues

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Determines which row was selected (e.g. which fighters, say element 1 points to fighter1)
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                //   let stud = StudentsArray[indexPath.row]




                let fighter: Fighter
                if searchController.isActive && searchController.searchBar.text != "" {
                    fighter = filteredFighters[indexPath.row]
                } else {
                    fighter = fighterArray[indexPath.row]
                }



                let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController

                // Makes sure selected student points to the correct controller
                controller.detailItem = fighter

                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }
    // MARK: - Table View

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

        if searchController.isActive && searchController.searchBar.text != "" {
            return filteredFighters.count
        }
        return fighterArray.count
    }

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

        let fighter: Fighter

        if searchController.isActive && searchController.searchBar.text != "" {
            fighter = filteredFighters[indexPath.row]
        } else {
            fighter = fighterArray[indexPath.row]
        }

        cell.textLabel!.text = fighter.name
        cell.detailTextLabel?.text = fighter.record

        cell.imageView?.image = UIImage(named: fighter.fighterImage)


        self.tableView .sizeToFit()
        // cell.detailTextLabel?.text = "fighterTest"
        return cell
    }


    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return false
    }



}

0 个答案:

没有答案