Reputation: 975
I need to implement alphabetic scrollbar like in music app . Can anyone help me with that code . As i searched on internet and was not able to get that code anywhere.
I need that scrollbar on uitableview in right side
i need scrollbar like this one.
Upvotes: 2
Views: 6097
Reputation: 82769
@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;
// }
need reference follow this tutorial
Upvotes: 1
Reputation: 820
I used it following way to display alphabet scroll bar and also go to that section when clicked on any alphabet. Note: My table view has n sections with each section having only 1 row ( thats my requirement). So clicking on any of the character will scroll to that section. This also takes care of case where you click on an alphabet which does not have matching results.
For example, you click on F
. There is no entry in indexSectionTitles starting with F
, its there only till D ( Doll ). So i will try to move to a section before F
i.e. E
. So this goes on and use will be taken to section with nearest character before it.
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)
}
Upvotes: 1