使用 swift 为 UIPickerView 编入 List/Side 字母表
Indexed List/Side Alphabet for UIPickerView using swift
我是 Swift 的新手,已将数组按字母顺序排序,我想知道如何实现与 [=22= 的联系人列表类似的索引列表或辅助字母表] 在 Swift。是否可以在 Swift 中使用类似于此的 UIPickerView 执行此操作:index for UIPickerView like UITableView?
这可能吗?
这是我在 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 { [=12=].localizedCaseInsensitiveCompare() == 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)
}
}
我使用了 UICollectionView 并自定义了流布局,使其类似于 UIPickerView,然后将字母表列表添加到旁边。
我是 Swift 的新手,已将数组按字母顺序排序,我想知道如何实现与 [=22= 的联系人列表类似的索引列表或辅助字母表] 在 Swift。是否可以在 Swift 中使用类似于此的 UIPickerView 执行此操作:index for UIPickerView like UITableView?
这可能吗?
这是我在 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 { [=12=].localizedCaseInsensitiveCompare() == 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)
}
}
我使用了 UICollectionView 并自定义了流布局,使其类似于 UIPickerView,然后将字母表列表添加到旁边。