And*_*nez 2 iphone objective-c uitableview uikit ios5
我想在我的应用程序中获取并索引所有iPod库艺术家,就像音乐应用程序一样.我遇到的问题是我不知道解决这个问题的最佳方法是什么.有帮助吗?
soo*_*per 15
让我们为你的UITableViewController班级命名LibraryArtistsBrowserTableViewController
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
Run Code Online (Sandbox Code Playgroud)
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)];
}
Run Code Online (Sandbox Code Playgroud)
下面定义了用于对项进行分区的函数,将其放在实现文件的某处:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
然后,我们确保视图控制器知道每个部分中有多少部分和行,以及每个部分的标题(索引字母/数字):
- (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];
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
你应该好好去.我唯一的问题是它无法逃脱标点符号(撇号等)和'''艺术家的前缀.除此之外它工作得很好.
| 归档时间: |
|
| 查看次数: |
4177 次 |
| 最近记录: |