2

我想对UITableView从生成的 a 进行基本排序MPMediaQuery

我想要的只是有一个基本的歌曲列表,将歌曲分类为 A、B、C、D 等部分。应该不难,但我尝试遵循的每个教程最终都没有给我这些结果一点也不。

任何帮助都会很棒。这是我的基本代码。我意识到这甚至还没有达到我想要完成的目标,但我认为经验丰富的开发人员从这一点开始帮助我会更容易,而不是我陷入困境。

我如何生成数据:

MPMediaQuery *musicLibraryQuery = [[MPMediaQuery alloc] init];
NSArray *musicLibraryArray = [musicLibraryQuery items];
songLibrary = [NSMutableArray arrayWithArray:musicLibraryArray];

这些是我的 UITableView 方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [songLibrary count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SongCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    MPMediaItem *song = [[songLibrary objectAtIndex:[indexPath row]] representativeItem];

    cell.textLabel.text = [song valueForProperty:MPMediaItemPropertyTitle];

    return cell;
}
4

3 回答 3

0

如果您的 Array 中有 Dictionary 作为对象,并且您想对特定键的数组进行排序,请执行以下操作:

静态 NSString* const keyToSortBy = @"keyToSortBy";

NSArray * sortedArray = [array sortedArrayUsingComparator:^(id obj1, id obj2) {
    NSString *s1 = [obj1 objectForKey:keyToSortBy];
    NSString *s2 = [obj2 objectForKey:keyToSortBy];
    return [s1 caseInsensitiveCompare:s2];
}];
于 2013-08-16T05:25:55.140 回答
0

例如,您需要对数组进行排序


songLibrary = [musicLibraryArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];


于 2013-08-16T04:54:37.257 回答
0

使用这个,也许这会有用

songLibrary=[songLibrary sortedArrayUsingSelector:@selector(compare:)];
于 2013-08-16T05:33:30.640 回答