Iphone 如何使用用户';什么是iPod库艺术家?
我想在我的应用程序中获取和索引所有iPod库艺术家,就像音乐应用程序一样。我遇到的问题是,我不知道解决这个问题的最佳方法是什么。有什么帮助吗?您应该阅读。您需要发出请求并用结果填充表视图。根据您的需要,您可以使用。如果你只是想要艺术家,你应该使用 编辑:Iphone 如何使用用户';什么是iPod库艺术家?,iphone,objective-c,uitableview,uikit,ios5,Iphone,Objective C,Uitableview,Uikit,Ios5,我想在我的应用程序中获取和索引所有iPod库艺术家,就像音乐应用程序一样。我遇到的问题是,我不知道解决这个问题的最佳方法是什么。有什么帮助吗?您应该阅读。您需要发出请求并用结果填充表视图。根据您的需要,您可以使用。如果你只是想要艺术家,你应该使用 编辑: 代表项目的文档说明: 集合中的媒体项通常共享公共属性 值,这取决于集合的构建方式。例如,如果你 基于使用 MPMediaItemPropertyTitist属性,集合共享中的所有项 同一个艺术家的名字。您可以使用representativeIt
代表项目的文档说明:
集合中的媒体项通常共享公共属性
值,这取决于集合的构建方式。例如,如果你
基于使用
MPMediaItemPropertyTitist属性,集合共享中的所有项
同一个艺术家的名字。您可以使用representativeItem属性
更有效地获取此类公共属性的值
比从items数组中获取项更有效
可能需要查看UILocalizedIndexedCollation
- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for(int i = 0; i < sectionCount; i++)
{
[unsortedSections addObject:[NSMutableArray array]];
}
for (id object in array)
{
NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
for (NSMutableArray *section in unsortedSections)
{
[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
SectionArray是h.中声明的NSArray
我认为这只适用于一系列歌曲。让我们来命名您的UITableViewController
classLibraryArtistsBrowserTableViewController
1。准备
接口文件LibraryArtistsBrowserTableViewController.h将包含以下变量:
@interface LibraryArtistsBrowserTableViewController : UITableViewController
@property (nonatomic, strong) MPMediaQuery *artistsQuery;
@property (nonatomic, strong) NSArray *artistsArray,*sectionedArtistsArray;
@property (nonatomic, strong) UILocalizedIndexedCollation *collation;
- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector;
@end
2。索引
在实现文件LibraryArtistsBrowserTableViewController.m中,将以下代码放入viewDidLoad
方法中:
- (void)viewDidLoad
{
[super viewDidLoad];
//Make a query for artists
self.artistsQuery = [MPMediaQuery artistsQuery];
//Group by Album Artist
[self.artistsQuery setGroupingType:MPMediaGroupingAlbumArtist];
//Grab the "MPMediaItemCollection"s and store it in "artistsArray"
self.artistsArray = [self.artistsQuery collections];
//We then populate an array "artists" with the individual "MPMediaItem"s that self.artistsArray` contains
NSMutableArray *artists = [NSMutableArray array];
for (MPMediaItemCollection *artist in artistsArray) {
//Grab the individual MPMediaItem representing the collection
MPMediaItem *representativeItem = [artist representativeItem];
//Store it in the "artists" array
[artists addObject:representativeItem];
}
//We then index the "artists" array and store individual sections (which will be the alphabet letter and a numbers section), all containing the corresponding MPMediaItems
self.sectionedArtistsArray = [self partitionObjects:artists collationStringSelector:@selector(albumArtist)];
}
我们用于划分项目的函数定义如下,请将其放置在实现文件的某个位置:
- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
self.collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for(int i = 0; i < sectionCount; i++)
[unsortedSections addObject:[NSMutableArray array]];
for (id object in array)
{
NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
for (NSMutableArray *section in unsortedSections)
[sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
return sections;
}
3。展示艺术家
然后,我们将艺术家展示如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//We grab the MPMediaItem at the nth indexPath.row corresponding to the current section (or letter/number)
MPMediaItem *temp = [[self.sectionedArtistsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
//Title the current cell with the Album artist
cell.textLabel.text = [temp valueForProperty:MPMediaItemPropertyAlbumArtist];
return cell;
}
你该走了。我唯一的问题是它无法摆脱标点符号(撇号等)和艺术家的前缀。除此之外,它工作得很好。是的,去过那里,做了所有这些。特别困难的是,实际使用查询提供的所有内容为UITableView编制索引。4周前,当我第一次尝试此功能时,当我得知查询没有按字母顺序将对象放入自己的数组时,我感到失望。“我想获取并索引我应用程序中的所有iPod库艺术家”,在你的问题中说出你已经做了什么,并更清楚地定义你的问题,这是很有用的。-我大部分时间都试着玩收藏品和收藏品部分,但没有办法用它们按字母顺序找到艺术家。-我试着制作一个NSArray的NSArray:每个子数组将包含所有以给定字母开头的艺术家。这种方法的问题是,当试图获取每个艺术家的第一个字母时,以“the”、“a”和其他字母开头的艺术家会破坏整个索引。好的,这是实际获取的工作。但实际的索引仍然有问题,哈哈。你仍然有这个问题吗?因为我相信我有一个解决办法给你。如果你这样做,我会爱你的。我做了一个解决方案,但速度非常慢(通过检查文章单词,通过分隔字符…)。它也很慢,所以是的,我很想听听你的解决方案。如何在选择手机时播放歌曲?有什么帮助吗?看起来你不会在viewDidLoad之外使用Artistaray;你也许可以去掉这个属性。你是如何索引歌曲的?如果要在tableview中选择一首歌曲,则排序规则的indexpath将只返回一首歌曲。完整的艺术家数组是不可能的,因为它对indexpath.section没有任何价值。此解决方案的工作原理与上面的Sooper解决方案相同。你问过如何给图书馆编索引,我告诉过你。不要仅仅因为你想一直握着你的手就否决一个解决方案。医生们来这里是有原因的。呵呵,我对此深表歉意,而且为时已晚。老实说,我不知道为什么我否决了你的答案。我从来没有想过要把整个代码放在一个银盘里或任何东西里,当时任何指针都是有用的。-我再次感到抱歉,我真的不记得为什么我否决了这个,除非你修改你的答案,否则我无法“修复”这个问题。我再次感到抱歉。
- (void)viewDidLoad
{
[super viewDidLoad];
//Make a query for artists
self.artistsQuery = [MPMediaQuery artistsQuery];
//Group by Album Artist
[self.artistsQuery setGroupingType:MPMediaGroupingAlbumArtist];
//Grab the "MPMediaItemCollection"s and store it in "artistsArray"
self.artistsArray = [self.artistsQuery collections];
//We then populate an array "artists" with the individual "MPMediaItem"s that self.artistsArray` contains
NSMutableArray *artists = [NSMutableArray array];
for (MPMediaItemCollection *artist in artistsArray) {
//Grab the individual MPMediaItem representing the collection
MPMediaItem *representativeItem = [artist representativeItem];
//Store it in the "artists" array
[artists addObject:representativeItem];
}
//We then index the "artists" array and store individual sections (which will be the alphabet letter and a numbers section), all containing the corresponding MPMediaItems
self.sectionedArtistsArray = [self partitionObjects:artists collationStringSelector:@selector(albumArtist)];
}
- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
self.collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for(int i = 0; i < sectionCount; i++)
[unsortedSections addObject:[NSMutableArray array]];
for (id object in array)
{
NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
for (NSMutableArray *section in unsortedSections)
[sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
return sections;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.collation sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.collation sectionIndexTitles];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.collation sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.sectionedArtistsArray objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//We grab the MPMediaItem at the nth indexPath.row corresponding to the current section (or letter/number)
MPMediaItem *temp = [[self.sectionedArtistsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
//Title the current cell with the Album artist
cell.textLabel.text = [temp valueForProperty:MPMediaItemPropertyAlbumArtist];
return cell;
}