section index重叠搜索栏(swift)

时间:2016-09-20 08:30:28

标签: ios swift uitableview uisearchcontroller

import UIKit

class orderPo: UITableViewController{
var sections : [(index: Int, length :Int, title: String)] = Array()
var array = ["Moscow", "@Saint Petersburg", "Novosibirsk", "Novosibirsk", "Nizhny Novgorod", "Samara", "Omsk", "Kiyv", "Odessa", "@Donetsk", "Harkiv", "Lviv", "Uzhgorod", "Zhytomyr", "Luhansk", "Mikolayv", "Kherson", "Germany", "Berlin", "Hamburg", "Munich", "Cologne", "Frankfurt", "Stuttgart", "Düsseldorf", "Dortmund", "Essen", "Bremen", "Abilene", "Akron", "Albuquerque", "Alexandria", "Allentown", "Amarillo", "Anaheim", "Anchorage", "Ann Arbor", "Antioch", "Arlington", "Arvada", "Athens", "Atlanta", "Augusta", "Aurora", "Austin", "Bakersfield", "Baltimore", "Baton Rouge", "Beaumont", "Bellevue", "Berkeley", "Billings", "Birmingham", "Boise", "Boston", "Boulder", "Bridgeport", "Broken Arrow", "Brownsville", "Buffalo", "Burbank", "Cambridge", "Cape Coral", "Carlsbad", "Carrollton", "Cary", "Cedar Rapids", "Centennial", "Chandler", "Charleston", "Charlotte", "Chattanooga", "Chesapeake", "Chicago", "Chula Vista", "Cincinnati", "Clarksville", "Clearwater", "Cleveland", "College Station", "Colorado Springs", "Columbia", "Columbus", "Concord", "Coral Springs", "Corona", "Corpus Christi", "Costa Mesa", "Dallas", "Daly City", "Davenport", "Dayton", "Denton", "Denver", "Des Moines", "Detroit", "Downey", "Durham", "Edison", "El Cajon", "El Monte", "El Paso", "Elgin", "Elizabeth", "Elk Grove", "Erie", "Escondido", "Eugene", "Evansville", "Everett", "Fairfield", "Fargo", "Fayetteville", "Fontana", "Fort Collins", "Fort Lauderdale", "Fort Wayne", "Fort Worth", "Fremont", "Fresno", "Frisco", "Fullerton", "Gainesville", "Garden Grove", "Garland", "Gilbert", "Glendale", "Grand Prairie", "Grand Rapids", "Green Bay", "Greensboro", "Gresham", "Hampton", "Hartford", "Hayward", "Henderson", "Hialeah", "High Point", "Hollywood", "Honolulu", "Houston", "Huntington Beach", "Huntsville", "Independence", "Indianapolis", "Inglewood", "Irvine", "Irving", "Jackson", "Jacksonville", "Jersey City", "Joliet", "Kansas City", "Kent", "Killeen", "Knoxville", "Lafayette", "Lakeland", "Lakewood", "Lancaster", "Lansing", "Laredo", "Las Cruces", "Las Vegas", "Lewisville", "Lexington", "Lincoln", "Little Rock", "Long Beach", "Los Angeles", "Louisville", "Lowell", "Lubbock", "Madison", "Manchester", "McAllen", "McKinney", "Memphis", "Mesa", "Mesquite", "Miami", "Miami Gardens", "Midland", "Milwaukee", "Minneapolis", "Miramar", "Mobile", "Modesto", "Montgomery", "Moreno Valley", "Murfreesboro", "Murrieta", "Naperville", "Nashville", "New Haven", "New Orleans", "New York", "Newark", "Newport News", "Norfolk", "Norman", "North Charleston", "North Las Vegas", "Norwalk", "Oakland", "Oceanside", "Oklahoma City", "Olathe", "Omaha", "Ontario", "Orange", "Orlando", "Overland Park", "Oxnard", "Palm Bay", "Palmdale", "Pasadena", "Paterson", "Pearland", "Pembroke Pines", "Peoria", "Peoria", "Philadelphia", "Phoenix", "Pittsburgh", "Plano", "Pomona", "Pompano Beach", "Port St. Lucie", "Portland", "Providence", "Provo", "Pueblo", "Raleigh", "Rancho Cucamonga", "Reno", "Rialto", "Richardson", "Richmond", "Riverside", "Rochester", "Rockford", "Roseville", "Round Rock", "Sacramento", "Saint Paul", "Salem", "Salinas", "Salt Lake City", "San Antonio", "San Bernardino", "San Diego", "San Francisco", "San Jose", "San Mateo", "Santa Ana", "Santa Clara", "Santa Clarita", "Santa Maria", "Santa Rosa", "Savannah", "Scottsdale", "Seattle", "Shreveport", "Simi Valley", "Sioux Falls", "South Bend", "Spokane", "Springfield", "St. Louis", "St. Petersburg", "Stamford", "Sterling Heights", "Stockton", "Sunnyvale", "Surprise", "Syracuse", "Tacoma", "Tallahassee", "Tampa", "Temecula", "Tempe", "Thornton", "Thousand Oaks", "Toledo", "Topeka", "Torrance", "Tucson", "Tulsa", "Tyler", "Vallejo", "Vancouver", "Ventura", "Victorville", "Virginia Beach", "Visalia", "Waco", "Warren", "Washington", "Waterbury", "West Covina", "West Jordan", "West Palm Beach", "West Valley City", "Westminster", "Wichita", "Wichita Falls", "Wilmington", "Winston–Salem", "Woodbridge", "Worcester", "Yonkers", "York"]
var filterArray :[String] = []

func filterContentForSearchText(searchText: String, scope: String = "All"){
    filterArray = array.filter{ text in return text.lowercaseString.containsString(searchText.lowercaseString)
}
    if searchController.active && searchController.searchBar.text != ""{
        sections = []
        filterArray.sortInPlace { $0 < $1 } // sort by alphabetic
        var index = 0;
        for ( var i = 0; i < filterArray.count; i++ ) {
            let commonPrefix = filterArray[i].commonPrefixWithString(filterArray[index], options: .CaseInsensitiveSearch)
            if (commonPrefix.isEmpty) {
                let string = filterArray[index].uppercaseString;
                let firstCharacter = string[string.startIndex]
                let title = "\(firstCharacter)"
                let newSection = (index: index, length: i - index, title: title)
                sections.append(newSection)
                index = i;
            }

        }
    }
    tableView.reloadData()
}

let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {


    array.sortInPlace { $0 < $1 } // sort by alphabetic
    var index = 0;
    for ( var i = 0; i < array.count; i++ ) {
        let commonPrefix = array[i].commonPrefixWithString(array[index], options: .CaseInsensitiveSearch)
        if (commonPrefix.isEmpty) {
            let string = array[index].uppercaseString;
            let firstCharacter = string[string.startIndex]
            let title = "\(firstCharacter)"
            let newSection = (index: index, length: i - index, title: title)
            sections.append(newSection)
            index = i;
        }
        }

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


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return sections.count

}

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

    return sections[section].length

}

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell;
    if searchController.active && searchController.searchBar.text != ""{
    cell.textLabel?.text = filterArray[sections[indexPath.section].index + indexPath.row]
    }
    else{
    cell.textLabel?.text = array[sections[indexPath.section].index + indexPath.row]
    }
    return cell

}

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

    return sections[section].title

}


override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
    return sections.map { $0.title }
}


override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {

    return index

}


}

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

如何设置节索引栏的高度?它现在与搜索栏重叠。它涵盖了搜索栏。如何在搜索栏下方创建部分索引栏。怎么做?任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

SearchController不支持ScopeBar。它是一个控制器,可以为您处理简单的用例。如果您需要范围栏,则应在故事板中添加UISearchBar组件,并通过实现其委托功能来处理搜索。