![](/img/trans.png)
[英]How can I implement the side alphabetic view in apple's music app in swift? Which used to sort the items in the list in alphabetic order
[英]how to implement alphabetic scrollbar like in music player app ios /
@interface yourViewcontroller ()
{
NSArray * IndexTitles;
}
- (void)viewDidLoad
{
[super viewDidLoad];
IndexTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return IndexTitles;
}
// - (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
// {
// return index;
// }
需要參考本教程
我按照以下方式使用它來顯示字母滾動條,並在單擊任何字母時也轉到該部分。 注意:我的表格視圖有n個部分,每個部分只有1行(這就是我的要求)。 因此,單擊任意字符將滾動到該部分。 這也可以照顧您單擊沒有匹配結果的字母的情況。
例如,您單擊F
在indexSectionTitles中沒有以F
開頭的條目,它僅直到D(Doll)為止。 所以我會嘗試之前移動到部分F
即E
。 因此,這繼續進行,並且將使用前面具有最近字符的部分。
var indexTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var indexSectionTitles: [String] = ["Apple", "Ball", "Car", "Doll"]
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
if indexSectionTitles.indexOf(title) != nil {
return indexSectionTitles.indexOf(title)!
}
else {
let char = title as NSString
let prevCharIndex = getPrevName(char)
print("prevCharIndex; ", prevCharIndex)
return prevCharIndex
}
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return indexTitles
}
func getPrevName(title: NSString) -> Int {
if title.isEqualToString("") {
return 0
}
let charInt = title.characterAtIndex(0) - 1
let charStr = String(UnicodeScalar(charInt))
if indexSectionTitles.indexOf(charStr) != nil {
return indexSectionTitles.indexOf(charStr)!
}
return getPrevName(charStr)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.