我是Swift的新手,并将数组按字母顺序排序,我想知道如何实现索引列表或侧面字母表,类似于Swift中IOS的联系人列表。是否可以在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 { $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 :(得分:0)
我使用了UICollectionView并自定义了流程布局,因此它类似于UIPickerView,然后将字母表列表添加到一边。