我正在尝试用objective-c在iOS上构建一个媒体浏览器。到目前为止,我可以获得songsQuery:
_query = [MPMediaQuery songsQuery];在我的tableView数据源中,我可以获得章节的数量和章节标题,如下所示:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(_query)
{
return _query.itemSections.count;
}
return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return _query.itemSections[section].title;
}这给我(比如说) 26个部分,标题像"A","B","C“等等……不出所料。我不明白的是如何获得每个部分的歌曲数量。即如何获得"A“或"B”等部分的所有歌曲
发布于 2020-12-09 00:31:30
如果我没理解错的话,您想要得到每个MPMediaQuery部分中的歌曲数量。
在documentation中,MPMediaQuerySection应该包含一个range属性,该属性用于存储节中包含的项数组的范围。然后,可以通过取该范围的length来获得歌曲的数量。如下所示:
NSUInteger numSongs = _query.itemSections[section].range.length;您还应该能够获得该部分中所有歌曲的数组,如下所示:
//The range of the song in the section
NSRange *sectionRange = _query.itemSections[section].range;
//Array of MPMediaItems in the section
NSArray *songsInSection = [_query.items subarrayWithRange: sectionRange];https://stackoverflow.com/questions/65200806
复制相似问题