tableView索引编号为#?

时间:2014-07-03 18:15:11

标签: ios uitableview

我有一个索引tableView,显示用户iPod库中的歌曲列表。但这些部分以1,2等开头,因为我的歌曲以数字开头。 如何将以数字开头的所有歌曲分组到一个部分'#'在我的索引中?

现在这是我的应用程序(左)与我喜欢的应用程序(右,Music.app)相比。我已经突出了#应该去哪里(在我的索引末尾):

Examples

到目前为止,这是我的代码viewDidLoad

songsQuery = [MPMediaQuery songsQuery];
songs = [songsQuery items];

NSMutableDictionary *songsInitials;

songsInitials = [[NSMutableDictionary alloc]init];
self.alphabetArray = [[NSMutableArray alloc] init];

for (int i=0; i< songs.count; i++)
{
    NSString *firstletter=[[[songs objectAtIndex:i] valueForProperty:MPMediaItemPropertyTitle] substringToIndex:1];

    if (songsInitials[firstletter] == nil) {
            songsInitials[firstletter] = [[NSMutableArray alloc] init];
            [self.alphabetArray addObject:firstletter];
    }  

    [songsInitials[firstletter] addObject:songs[i]];

}

[self.alphabetArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];   //sorting array in ascending array

self.songsInitials = songsInitials;

....和我的tableView数据来源:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _alphabetArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [_songsInitials[_alphabetArray[section]] count];
}

-(NSString *)titleForRow:(NSIndexPath *)indexpath{

    NSString *firstLetter = _alphabetArray[indexpath.section];
    MPMediaItem *myItem = [_songsInitials[firstLetter] objectAtIndex:indexpath.row];

    return [myItem valueForProperty:MPMediaItemPropertyTitle];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.alphabetArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    NSIndexPath *indexpath;

    for (int i=0; i < self.alphabetArray.count; i++)
    {
        NSString *titleToSearch=[self.alphabetArray objectAtIndex:i];  //getting sectiontitle from array
        if ([title isEqualToString:titleToSearch])  // checking if title from tableview and sectiontitle are same
        {
            indexpath=[NSIndexPath indexPathForRow:0 inSection:i];
            // scrolling the tableview to required section
            [self.songsTable scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];
            break;
        }
    }
    return indexpath.section;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.text= [self titleForRow:indexPath];  //getting cell content

    return cell;
}

这是我迄今为止尝试过的,调整for loop中的viewDidLoad

for (int i=0; i< songs.count; i++)
    {
        NSString *firstletter=[[[songs objectAtIndex:i] valueForProperty:MPMediaItemPropertyTitle] substringToIndex:1];
        if (songsInitials[firstletter] == nil) {
            NSString *songsInitialsFirstLetter = songsInitials[firstletter];

            if ([self stringIsNumeric:songsInitialsFirstLetter]){
                songsInitials[firstletter] = [[NSMutableArray alloc] init];
                [self.alphabetArray addObject:[NSString stringWithFormat:@"#"]];
                NSLog(@"There are numbers!");
            }
            else if (![self stringIsNumeric:songsInitialsFirstLetter]){
                songsInitials[firstletter] = [[NSMutableArray alloc] init];
                [self.alphabetArray addObject:firstletter];
            }

        }

        [songsInitials[firstletter] addObject:songs[i]];

    }

-(BOOL) stringIsNumeric:(NSString *) str {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [formatter numberFromString:str];
    return !!number; // If the string is not numeric, number will be nil
}

......但它没有用。我已经四处搜索了,但我找不到任何其他问题,我在这里错过了一些明显的东西吗?任何帮助将非常感激! :)

1 个答案:

答案 0 :(得分:1)

没有测试但是在查看数组之前更改第一个字母会产生更好的结果。 排序后,不确定符号(#)将在哪里结束,你可能需要手动将它推到最后。

for (int i=0; i< songs.count; i++)
{
    NSString *firstletter=[[[songs objectAtIndex:i] valueForProperty:MPMediaItemPropertyTitle] substringToIndex:1];
    NSScanner *ns = [NSScanner scannerWithString:firstletter];
    if ( [ns scanInt:NULL] )
    {
         firstLetter = @"#";
    }

    if (songsInitials[firstletter] == nil) {
        songsInitials[firstletter] = [[NSMutableArray alloc] init];
        [self.alphabetArray addObject:firstletter];
    }  

    [songsInitials[firstletter] addObject:songs[i]];

}