0

我正在编写一个应用程序,该应用程序通过 NSMutableArray nd NSdictionary 用法将从 mysql 数据库检索到的数据加载到表视图中。而且我担心我在 NSDictionary 位中犯了一个错误。

我有 2 个控制 NSMutableArrays 可以正确加载到表格单元格中,但是一旦我尝试拆分收到的 NSDictionary 并将键“名称”和“艺术家”中的数据加载到单元格中,它就会崩溃。请帮我识别错误。

我知道问题不在于 php 或字典,因为我记录了收到的字典并且输出非常完整。(粘贴在下面)

2012-08-28 15:53:52.339 es[11783:c07] {
Artist = "K-Phlowz";
Likes = 27;
Link = "http://localhost/SNTestFiles/Music/K-PHLOWZ-BENOITSHIT.mp3";
Name = "Benoit Shit";
PhotoLink = "http://localhost/SNTestFiles/Photos/TopMusic/K-PHLOWZ-BENOITSHIT.jpg";
}

但是,当我在 tableviewcell 方法中声明 NSDictionary 时,它会崩溃,因此当我尝试将“name”和“Artist”中的字符串加载到 cell.labeltext 和 cell.detaillabeltext 中时,它不起作用。请帮助我确定问题和/或让我知道是否有更准确的方法来解决此问题。(源代码粘贴在下面)。

随意复制并粘贴到您的 xcode 中,以帮助清楚地识别问题..

// ChanViewController.h
// es
//
// Created by Nnamdi Okeke on 8/7/12.
// Copyright (c) 2012 Bubble Technology. All rights reserved.
//

#import <UIKit/UIKit.h>

#define kGETUrl @"http://localhost/SNTestFiles/PHPFiles/FetchSongs.php?Artist=%@"

@interface ChanViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

{
NSDictionary *stuff;
IBOutlet UILabel *label;
NSMutableArray *jams;
NSMutableArray *artists;
NSMutableArray *json;
IBOutlet UITableView *songs;
IBOutlet UILabel *artist;
IBOutlet UIImageView *ArtistCovers;
} 
@property (nonatomic, retain) UIImageView *ArtistCovers;

-(IBAction)back:(id)sender;

@end


// ChanViewController.m
// es
//
// Created by Nnamdi Okeke on 8/7/12.
// Copyright (c) 2012 Bubble Technology. All rights reserved.
//

#import "ChanViewController.h"
#import "AppDelegate.h"
#import "ViewController.h"




@implementation ChanViewController

@synthesize ArtistCovers; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}


- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{

//--------------------------------passing in data from homepage---------------

AppDelegate *artistname = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[artist setText:artistname.artistsnames];



//----------------------------------Fetchin Cover Photos---------------------------

NSString *url = [NSString stringWithFormat:@"http://localhost/SNTestFiles/Photos/ArtistCovers/%@.jpg", artist.text];

UIImage *ArtistCover = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];

[ArtistCovers setImage:ArtistCover];

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.





//----------------------------test nsmutablearrays-------------------------------

jams = [[NSMutableArray alloc] initWithObjects:@"Medusa", @"Toyin", @"So Incredible", @"I Don Hammer",@"Dami Duro", @"Senrenre", @"Sisi Nene", @"Benoit Shit", @"Dance For Me", @"Tom Ford Flow", @"Magic", @"Gidi Swag", @"Aunty Dupe", nil];

artists = [[NSMutableArray alloc] initWithObjects:@"Tobias", @"DRBLasgidi", @"JRah", @"T-Topz",@"Davido", @"Ajebutter22", @"Wizkid", @"K-Phlowz", @"Wizkid", @"DRBLasgidi", @"L.O.S", @"Ajebutter22", @"Joules Da Kidd", nil];

//---------------------------------------------------------------------------------------------



NSString *fetchjamsurl = [NSString stringWithFormat:kGETUrl, artist.text];
NSData *fetchjamsdata = [NSData dataWithContentsOfURL:[NSURL URLWithString:fetchjamsurl]];
NSError *e;
json = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:fetchjamsdata options:kNilOptions error:&e];
NSDictionary *info = [json objectAtIndex:0];
NSLog(@"%@", info);
label.text = [info objectForKey:@"Artist"];

}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}


/////////////////////// -- THE PROBLEM -- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                                                        \\
//                                                                        //
    stuff = (NSDictionary *)[json objectAtIndex:indexPath.row];
//                                                                        \\
//                                                                        \\
////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


//-------------------------test strings for cells-----------------------

NSString *SongTitle = [[jams objectAtIndex:indexPath.row] objectForKey:@"Name"];
NSString *ArtistNames = [[artists objectAtIndex:indexPath.row] objectForKey:@"Artist"];

//------------------------------------------------------------------------


NSString *SongTitlejson = [stuff objectForKey:@"Name"];
NSString *ArtistNamesjson = [stuff objectForKey:@"Artist"];



cell.textLabel.text = SongTitlejson;
cell.detailTextLabel.text = ArtistNamesjson;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:@"Heiti TC" size:18];
cell.detailTextLabel.font = [UIFont fontWithName:@"Heiti TC" size:11];
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];



UIButton *AddToPlaylist = [UIButton buttonWithType:UIButtonTypeCustom];
AddToPlaylist.frame = CGRectMake(240, 5, 30, 30);
[AddToPlaylist setImage:[UIImage imageNamed:@"addtoplaylist.png"] forState:UIControlStateNormal];
[AddToPlaylist addTarget:self action:@selector(AddToPlaylists:) forControlEvents:UIControlEventTouchUpInside];

UIButton *LikeSong = [UIButton buttonWithType:UIButtonTypeCustom];
LikeSong.frame = CGRectMake(280, 5, 30, 30);
[LikeSong setImage:[UIImage imageNamed:@"CellLike.png"] forState:UIControlStateNormal];
[LikeSong addTarget:self action:@selector(Like:) forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:AddToPlaylist];
[cell addSubview:LikeSong];

return cell;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

> ///////////////////////////////////////// ////////////////////////////////////

非常感谢伙计们

4

1 回答 1

0

好的。您的问题的解决方案是在 .h 中声明内容,并在 @property 和 @synthesize 中声明它,或者在 -init 中使用NSDictionary *stuff = [NSDictionary new];或类似的方式对其进行初始化。然后你可以设置stuff(NSDictionary *)[json objectAtIndex:indexPath.row];没有问题。(这是假设 json 可以与 NSDictionary 互换。)

请注意,问题是东西还没有被初始化,所以编译器在运行时没有为它分配任何内存。当然,另一种可能性是您的 json 对象与 NSDictionary 类不兼容。

于 2012-08-31T16:11:49.200 回答