我正在创建一个基本的音乐应用程序 - 首先在UICollectionView
中显示乐队的专辑,然后当我点击一个专辑时,我会被推到详细视图,该视图显示有关选定的专辑,以及专辑曲目列表。该应用程序的目的是能够点击一个tracklist单元格,然后播放该歌曲。
我有这些大量的阵列,其中包含所有专辑的完整曲目列表,而不是很酷,太大。
有人告诉我,我可以使用Plist文件。但是,我想知道如何将所有信息都放在一个 plist文件中,因为我有几张专辑,因此有几个跟踪列表。我应该创建一个列出我所有相册的plist文件吗?那我应该在哪里插入跟踪列表呢?
你能指点我一个解决方案吗?
这是我的故事板的截图:
Click here to view screenshot full screen.
我是初学者,想要优化我的代码。我还无法在单元格中播放这首歌。
CollectionViewController.h:
#import <UIKit/UIKit.h>
@interface CollectionViewController : UICollectionViewController
@property (strong, nonatomic) NSArray *albumImages;
@property (strong,nonatomic) NSArray *albumDescriptions;
@end
CollectionViewController.m:
#import "CollectionViewController.h"
#import "Cell.h"
#import "ImageDetailViewController.h"
@interface CollectionViewController ()
@end
@implementation CollectionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.albumImages = @[@"1998_Yellow.png",@"1998_WoM",@"2000_PoTY",@"2001_SFA",@"2003_MFZB",@"2004_WoMFZB",@"2006_BttW",@"2008_NtnA",@"2008_Phoenix",@"2009_PantyRaid",@"2011_GetNice",@"2013_CyF"];
self. albumDescriptions = @[@"Yellow [1998]",@"Waste of Mind [1998]",@"Playmate of the Year [2000]",@"Stupid Fat Americans - EP [2001]",@"MFZB [2003]",@"Waste of MFZB [2004]",@"Broadcast to the World [2006]",@"Not the New Album - EP [2008]",@"Phoenix [2008]",@"Panty Raid [2009]",@"Get Nice! [2011]",@"Call Your Friends [2013]" ];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.albumImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
UIImage *albumImage = [[UIImage alloc] init];
albumImage = [UIImage imageNamed:[self.albumImages objectAtIndex:indexPath.row]];
cell.imageView.image = albumImage;
return cell;
NSLog(@"Cellule cliquée");
}
#pragma mark - Prepare for Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UICollectionViewCell *cell = (UICollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSLog(@"Segue versDetails");
ImageDetailViewController *imageDetailViewController = (ImageDetailViewController *)segue.destinationViewController;
imageDetailViewController.albumImage = [UIImage imageNamed:[self.albumImages objectAtIndex:indexPath.row]];
imageDetailViewController.albumLabelText = [self. albumDescriptions objectAtIndex:indexPath.row];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewDidDisappear: (BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ImageDetailViewController.h:
@interface ImageDetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *albumImageView;
@property (strong, nonatomic) IBOutlet UILabel *albumDetailLabel;
@property (strong, nonatomic) UIImage *albumImage;
@property (strong, nonatomic) NSString *albumLabelText;
@property (strong,nonatomic) NSArray *tracklist_Y;
@property (strong,nonatomic) NSArray *tracklist_WoM;
@property (strong,nonatomic) NSArray *tracklist_PotY;
@property (strong,nonatomic) NSArray *tracklist_SFA;
@property (strong,nonatomic) NSArray *tracklist_MFZB;
@property (strong,nonatomic) NSArray *tracklist_WoMFZB;
@property (strong,nonatomic) NSArray *tracklist_BttW;
@property (strong,nonatomic) NSArray *tracklist_NtnA;
@property (strong,nonatomic) NSArray *tracklist_Ph;
@property (strong,nonatomic) NSArray *tracklist_PR;
@property (strong,nonatomic) NSArray *tracklist_GN;
@property (strong,nonatomic) NSArray *tracklist_CyF;
@end
imageDetailViewController.m:
#import "ImageDetailViewController.h"
#import "animationY.h"
@interface ImageDetailViewController ()
@end
@implementation ImageDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
//return [self.tracklist_Y count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Liste des chansons (Tracklist)
//http://blog.safaribooksonline.com/2013/02/26/objective-c-how-to-populate-data-in-uitableviewcontroller/
self.tracklist_Y = [[NSArray alloc]initWithObjects:@"1. Check",@"2. All I Need",@"3. Swing",@"4. Walk Away",@"5. Bootylicious Vinyl",@"6. Hate",@"7. Mindtrip",@"8. Chrome",@"9. Jag Off",@"10. Song 10",nil];
self.tracklist_WoM = [[NSArray alloc]initWithObjects:@"1. Check",@"2. Get Back",@"3. The Real Me",@"4. Someday",@"5. Waste of Mind",@"6. Feel This Way",@"7. Walk Away",@"8. Big Shot",@"9. Swing",@"10. Jag Off",@"11. Time",@"12. Move On",@"13. Flay Daze",@"14. Bootylicious Vinyl",nil];
self.tracklist_PotY = [[NSArray alloc]initWithObjects:@"1. I Am",@"2. Playmate of the Year",@"3. Now or Never",@"4. Wasted",@"5. I'm Money",@"6. Go",@"7. What's Goin' On?",@"8. Subtract You",@"9. The Hell that is my Life",@"10. E Generation",@"11. Livin' Libido Loco",@"12. In My Room",nil];
self.tracklist_SFA = [[NSArray alloc]initWithObjects:@"1. Wasted",@"2. Chrome [Demo Version]",@"3. Swing [Demo Version]",@"4. Deck the Halls (I Hate Christmas) W/Intro",@"5. Jag Off [Live]",@"6. Someday [Live]",@"7. Get Back [Live]",nil];
self.tracklist_MFZB = [[NSArray alloc]initWithObjects:@"1. Rescue Me",@"2. Over the Edge",@"3. Strength",@"4. Hello Tomorrow",@"5. The Set-Up",@"6. Blur",@"7. House is Not my Home",@"8. Into You",@"9. Alone",@"10. Expectations",@"11. Falling Apart",@"12. Let it Ride",@"13. Type A",@"14. Runaway",@"15. Dear You (Far Away)",@"16. The Fear",@"17. Surrender [*]",@"18. Good Things [*]",@"19. Dissatisfied [*]",nil];
self.tracklist_WoMFZB = [[NSArray alloc]initWithObjects:@"1. Are You For Real?",@"2. Let Me Go",@"3. One Less Headache]",@"4. Burn the School Down",@"5. Lightning Rod",@"6. Blindside",@"7. Veils and Visions",@"8. One Shot",@"9. Timing is Everything",@"10. Wannabe",nil];
self.tracklist_BttW = [[NSArray alloc]initWithObjects:@"1. Broadcast to the World",@"2. Rated \"U\" for Ugly",@"3. Anthem",@"4. Enemy",@"5. Back to Normal",@"6. Postcards from Hell",@"7. Karma Flavored Whisky",@"8. Here's to You",@"9. Wake me Up",@"10. Lobotomy for Dummies",@"11. The Walking Dead",@"12. Your New Boyfriend Wears Girl Pants",@"13. Riot Girl [*]",@"14. Down in Flames [*]",@"15. Get on the Bus [*]",@"16. Hit it Again [*]",nil];
self.tracklist_NtnA = [[NSArray alloc]initWithObjects:@"1. Mental Health",@"2. Photographs",@"3. Politics",@"4. The Art of Breaking Up",@"5. We're Not a Cover Band, We're a Tribute Band",nil];
self.tracklist_Ph = [[NSArray alloc]initWithObjects:@"1. HMP",@"2. Hell Yeah!",@"3. Just the Tip",@"4. Mental Health",@"5. The Juggernauts",@"6. Death by Disco",@"7. Be Careful What You Wish For",@"8. Morse Code for Suckers",@"9. Ignite",@"10. Mike Dexter is a God, Mike Dexter is a Role Model, Mike Dexter is an A**hole",@"11. The Junkie and the Halo",@"12. Brixton",@"13. Hit the Ground",@"14. Two Wrongs don't make a Right, but Three Rights make a Left",@"15. All for None and None for All",@"16. Sorry, but Your Friends are Hot",@"17. The Art of Breaking Up [*]",@"18. We're Not a Cover Band, We're a Tribute Band [*]",nil];
self.tracklist_PR = [[NSArray alloc]initWithObjects:@"1. Survivor",@"2. Girls Just Want to Have Fun",@"3. Underneath it All",@"4. Trouble",@"5. London Bridge",@"6. Beautiful",@"7. Girlfriend",@"8. The Sweet Escape",@"9. Jenny from the Block",@"10. Rehab",@"11. Spice Up Your Life",@"12. Oops!... I Did it Again",@"13. Get the Party Started",@"14. Mickey",@"15. All I Want for Christmas is You [*]",@"16. Who Let the Dogs Out [*]",nil];
self.tracklist_GN = [[NSArray alloc]initWithObjects:@"1. Blackout",@"2. Nothing to Lose",@"3. She Don't Wanna Rock",@"4. Ricky Bobby",@"5. Get Nice!",@"6. The Joke's on You",@"7. Nudist Priest",@"8. Galileo Was Wrong",@"9. Truck Stops and Tail Lights",@"10. I'm Definitely Not Gonna Miss You",@"11. Too Bored to Bleed",@"12. Kiss your Ass Goodbye",@"13. This is Gonna Hurt You Way More than it's Gonna Hurt Me",@"14. Demon Days",@"15. Light Up the Sky [*]",@"16. A Freak Gasoline Fight Accident [*]",nil];
self.tracklist_CyF = [[NSArray alloc]initWithObjects:@"1. Sirens",@"2. I'm Just Here for the Free Beer",@"3. With Friends Like These, Who Needs Herpes?",@"4. Call Your Friends",@"5. Murder on the Airwaves",@"6. Public Enemy Number One",@"7. Born to Lose",@"8. Stick Em Up Kid!",@"9. Automatic",@"10. Nerd Armor",@"11. Panic in the Streets",@"12. Don't Believe the Hype",@"13. Until the Sun Comes Up",@"14. Last Call",@"15. Sex, Lies & Audiotape [*]",@"16. Battle of the Bullshit [*]",@"17. Ready Steady Go [*]",nil];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TrackCell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TrackCell"];
}
// Vérification de quel album a été cliqué;
// Il semble qu'il n'y ait pas moyen d'utiliser un Switch avec un NSString, selon StackOverflow : http://goo.gl/dpylqF
if ([_albumDetailLabel.text isEqualToString:@"Yellow [1998]"]) {
animationY * jouerAnimationY = [[animationY alloc]initWithFrame:CGRectMake(85, -20, 600, 600) prefixNom:@"yellowAnim" nbImages:120];
[self.view addSubview: jouerAnimationY ];
cell.textLabel.text = [self.tracklist_Y objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Yellow";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Waste of Mind [1998]"]) {
cell.textLabel.text = [self.tracklist_WoM objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Waste of Mind";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Playmate of the Year [2000]"]) {
cell.textLabel.text = [self.tracklist_PotY objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Playmate of the Year";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Stupid Fat Americans - EP [2001]"]) {
cell.textLabel.text = [self.tracklist_SFA objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Stupid Fat Americans";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"MFZB [2003]"]) {
cell.textLabel.text = [self.tracklist_MFZB objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"MFZB";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Waste of MFZB [2004]"]) {
cell.textLabel.text = [self.tracklist_WoMFZB objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Waste of MFZB";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Broadcast to the World [2006]"]) {
cell.textLabel.text = [self.tracklist_BttW objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Broadcast to the World";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Not the New Album - EP [2008]"]) {
cell.textLabel.text = [self.tracklist_NtnA objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Not the New Album";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Phoenix [2008]"]) {
cell.textLabel.text = [self.tracklist_Ph objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Phoenix";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Panty Raid [2009]"]) {
cell.textLabel.text = [self.tracklist_PR objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Panty Raid";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Get Nice! [2011]"]) {
cell.textLabel.text = [self.tracklist_GN objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Get Nice!";
return cell;*/
} else if ([_albumDetailLabel.text isEqualToString:@"Call Your Friends [2013]"]) {
cell.textLabel.text = [self.tracklist_CyF objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
return cell;
/*cell.textLabel.text = @"Call Your Friends";
return cell;*/
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = NO;
self.albumImageView.image = self.albumImage;
self.albumDetailLabel.text = self.albumLabelText;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:1)
我曾经使用视图控制器作为我的数据模型。
您的Plist的结构应该是这样的。
守则:
CollectionViewController.h :清空它。
<强> CollectionViewController.m:强>
#import "CollectionViewController.h"
#import "Cell.h"
#import "ImageDetailViewController.h"
@interface CollectionViewController ()
@property (nonatomic, strong) NSArray *albums;
@end
@implementation CollectionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//You dont want this.
/*self.albumImages = @[@"1998_Yellow.png",@"1998_WoM",@"2000_PoTY",@"2001_SFA",@"2003_MFZB",@"2004_WoMFZB",@"2006_BttW",@"2008_NtnA",@"2008_Phoenix",@"2009_PantyRaid",@"2011_GetNice",@"2013_CyF"];
self. albumDescriptions = @[@"Yellow [1998]",@"Waste of Mind [1998]",@"Playmate of the Year [2000]",@"Stupid Fat Americans - EP [2001]",@"MFZB [2003]",@"Waste of MFZB [2004]",@"Broadcast to the World [2006]",@"Not the New Album - EP [2008]",@"Phoenix [2008]",@"Panty Raid [2009]",@"Get Nice! [2011]",@"Call Your Friends [2013]" ];*/
//Do this if you are using a plist
//Makes everything thread happy (maximal responsiveness)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //Get path of application
NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents path
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //Get path to dictionary
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //Check if file exists
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //Locate file in bundle
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //Copy file over
}
self.albums = [[NSArray alloc] initWithContentsOfFile: path]; //Initalize dictionary from plist
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData]; //Reload your data
});
});
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.albums count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
UIImage *albumImage = [[UIImage alloc] init];
albumImage = [UIImage imageNamed:[[self.albums objectAtIndex:indexPath.row] objectForKey:@"Album Image"]];
cell.imageView.image = albumImage;
return cell;
NSLog(@"Cellule cliquée");
}
#pragma mark - Prepare for Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UICollectionViewCell *cell = (UICollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSLog(@"Segue versDetails");
ImageDetailViewController *imageDetailViewController = (ImageDetailViewController *)segue.destinationViewController;
imageDetailViewController.album = [self.albums objectAtIndex:indexPath.row];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewDidDisappear: (BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
<强> ImageDetailViewController.h:强>
@interface ImageDetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *albumImageView;
@property (strong, nonatomic) IBOutlet UILabel *albumDetailLabel;
/*@property (strong, nonatomic) UIImage *albumImage;
@property (strong, nonatomic) NSString *albumLabelText;*/
@property (nonatomic, strong) NSDictionary *album;
/*@property (strong,nonatomic) NSArray *tracklist_Y;
@property (strong,nonatomic) NSArray *tracklist_WoM;
@property (strong,nonatomic) NSArray *tracklist_PotY;
@property (strong,nonatomic) NSArray *tracklist_SFA;
@property (strong,nonatomic) NSArray *tracklist_MFZB;
@property (strong,nonatomic) NSArray *tracklist_WoMFZB;
@property (strong,nonatomic) NSArray *tracklist_BttW;
@property (strong,nonatomic) NSArray *tracklist_NtnA;
@property (strong,nonatomic) NSArray *tracklist_Ph;
@property (strong,nonatomic) NSArray *tracklist_PR;
@property (strong,nonatomic) NSArray *tracklist_GN;
@property (strong,nonatomic) NSArray *tracklist_CyF;*/
@end
<强> ImageDetailViewController.m 强>
#import "ImageDetailViewController.h"
@interface ImageDetailViewController ()
@end
@implementation ImageDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.album objectForKey:@"Album Tracklist"] count];
//return [self.tracklist_Y count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Liste des chansons (Tracklist)
//http://blog.safaribooksonline.com/2013/02/26/objective-c-how-to-populate-data-in-uitableviewcontroller/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TrackCell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TrackCell"];
}
cell.textLabel.text = [[self.album objectForKey:@"Album Tracklist"] objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = NO;
self.albumDetailLabel = [self.album objectForKey:@"Album Label"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
所以基本上在这里,你有一个你的专辑数组,当你为关键事物做对象时,你正在提取图像,标签和轨道列表。我尽力使你的代码更友好,但它可能不完美...