2016-04-23 62 views
1

我是Swift中的新手,并且按照字母顺序对数组进行了排序,并且想知道如何实现索引列表或旁边字母表,类似于IOS中的联系人列表在Swift中。是否有可能在Swift中使用UIPickerView类似如下:index for UIPickerView like UITableView?使用swift的UIPickerView的索引列表/边字母表

这可能吗?

我的继承人上的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) 
} 

}

回答

0

我用了一个UICollectionView和定制的流程布局,它类似于一个UIPickerView,然后添加字母列表到一边。