【问题标题】:how to implement alphabetic scrollbar like in music player app ios /如何在音乐播放器应用程序 ios / 中实现字母滚动条
【发布时间】:2015-06-23 11:50:55
【问题描述】:

我需要像在音乐应用中那样实现 字母滚动条。任何人都可以帮助我使用该代码。当我在互联网上搜索时,无法在任何地方获得该代码。

我需要右侧 uitableview 上的滚动条

我需要像这样的滚动条。

【问题讨论】:

  • 你能举一些例子如何需要
  • 是的,我也更新了。 @安布

标签: ios iphone uitableview scrollbar uislider


【解决方案1】:

我按照以下方式使用它来显示字母滚动条,并在单击任何字母时转到该部分。 注意:我的表格视图有 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)
}

【讨论】:

    【解决方案2】:
    @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;
    // }
    

    需要参考请关注此tutorial

    【讨论】:

    • 是的,我有那个滚动条,但是当我点击它时我需要滚动到索引的功能。例如:- 当我单击滚动条中的 S 时,它会将表格向下滚动到从字母 S 开始的人。@Anbu.Karthik
    • 你上面分享的链接里面有静态数据。但我在 uitable view 中获取 abadressbook。那么我该如何实现呢。 @Anbu.Karthik
    • 两者是相同的兄弟,当你在 ABAddressbook 中获取名称后,使用 NSSortDesiptor 进行排序,然后将数据加载到表中,简单
    • 简单我的朋友,上面的编码很好,不需要改变,你需要按照这个步骤,步骤1:当你从通讯录中获取名字,姓氏存储到数组中。第 2 步:在这里使用 NSSortdescriptor 对数组进行排序 第 3 步:当它结束时 [yourtableview reloadData]。就这些
    • 您需要添加sectionForSectionIndexTitle 方法以返回一个整数(这是部分编号),当您单击一个字符时要转到该值。看我的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多