Search code examples
iosswiftuitableviewuipickerview

Indexed List/Side Alphabet for UIPickerView using swift


I'm a newcomer in Swift and have sorted an array into alphabetical order and am wondering how to implement an indexed list or side alphabet similar to what is seen with the contact list for IOS in Swift. Is it possible to do so in Swift with UIPickerView similar to this: index for UIPickerView like UITableView?

Is this possible?

Heres my current code on Xcode:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource,UIPickerViewDelegate {

@IBOutlet weak var friendSearch: UISearchBar!
@IBOutlet weak var friendPicker: UIPickerView!



let friendNames = ["Lisa Young","Mary Jenkins","Sarah Miller","Tina Clark","Melissa Jackson","Christine Edwards","Jane Taylor","Carol Stewart","Anne Brown","Sandra Simmons","Nicole Sanchez","Wanda Baker","Janice Wood","Gloria Wilson","Catherine Perry","Julie Thomas","Laura Watson","Alice Torres","Diane Diaz","Katherine Reed","Mildred Morgan","Carolyn Cooper","Beverly Johnson","Dorothy Smith","Nancy Anderson","Karen Peterson","Sara Turner","Rachel Barnes","Pamela Wright","Susan Brooks","Kathleen Lopez","Patricia Hall","Kathryn Foster","Janet Morris","Denise King","Lillian Bryant","Amy Robinson","Anna Allen","Stephanie Mitchell","Barbara Walker","Jessica Cox","Annie Richardson","Margaret Collins","Joan Russell","Louise Rogers","Jacqueline Long","Donna Murphy","Frances Sanders","Kelly Gonzales","Sharon Bennett","Linda Washington","Ashley Roberts","Teresa Hughes","Christina Nelson","Marie Thompson","Brenda Alexander","Doris Williams","Michelle Harris","Martha Parker","Norma Gonzalez","Emily Flores","Angela Garcia","Julia Martin","Betty Campbell","Cynthia Martinez","Elizabeth Davis","Diana Moore","Rose Griffin","Kathy Butler","Theresa Adams","Joyce Gray","Maria White","Jennifer Perez","Debra Hill","Andrea Lewis","Marilyn Cook","Virginia Price","Lois Carter","Deborah Powell","Shirley Scott","Kimberly Ramirez","Rebecca Bell","Irene Green","Evelyn Howard","Paula Kelly","Ruby Bailey","Jean Hernandez","Amanda Phillips","Lori James","Phyllis Lee","Ruth Jones","Judy Ward","Heather Evans","Helen Ross","Bonnie Rodriguez","Tammy Henderson","Cheryl Rivera","Ann Patterson","Judith Coleman"]

var sortedFriends = []

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

    if (friendSearch.text != nil) {

    }

    sortedFriends = friendNames.sort { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
}

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

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return sortedFriends.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return (sortedFriends[row] as! String)
}

}


Solution

  • I used a UICollectionView and customized the flow layout so it resembles a UIPickerView and then added the alphabet list to the side.