2016-09-20 107 views
1
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!) 
} 
} 

如何設置段索引欄的高度?它現在與搜索欄重疊。它涵蓋了搜索欄。如何使搜索欄下方的部分索引欄。怎麼做?任何幫助,將不勝感激段索引與搜索欄重疊(swift)

回答

0

SearchController不旨在支持ScopeBar。它是一個爲您處理簡單用例的控制器。如果你需要一個範圍欄,你應該在故事板中添加一個UISearchBar組件,並通過實現它的委託功能來處理搜索。