Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试加载到tableviewcell时,NSdictionary应用程序崩溃_Ios_Xcode - Fatal编程技术网

Ios 尝试加载到tableviewcell时,NSdictionary应用程序崩溃

Ios 尝试加载到tableviewcell时,NSdictionary应用程序崩溃,ios,xcode,Ios,Xcode,我正在编写一个应用程序,通过NSMutableArray和NSdictionary用法将从mysql数据库检索到的数据加载到表视图中。我担心我在这一点上犯了错误 我有2个控制NSMutableArray,它们可以正确加载到表单元格中,但一旦我尝试拆分收到的NSDictionary并将键“Name”和“Artist”中的数据加载到单元格中,它就会崩溃。请帮我识别错误 我知道问题不在于php或字典,因为我记录了收到的字典,输出非常完整。(贴在下面) 但是,当我在tableviewcell方法中声明N

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

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

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

但是,当我在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
//ChanViewController.h
//es
//
//由Nnamdi Okeke于2012年8月7日创建。
//版权所有(c)2012 Bubble Technology。版权所有。
//
#进口
#定义kGETUrl@“http://localhost/SNTestFiles/PHPFiles/FetchSongs.php?Artist=%@"
@接口ChanViewController:UIViewController
{
字典里的东西;
IBUILABEL*标签;
NSMutableArray*阻塞;
NSMutableArray*艺术家;
NSMutableArray*json;
ibuitableview*歌曲;
IBUILabel*艺术家;
IBUIImageView*艺术家封面;
} 
@财产(非原子,保留)UIImageView*艺术家封面;
-(iAction)返回:(id)发送方;
@结束
//ChanViewController.m
//es
//
//由Nnamdi Okeke于2012年8月7日创建。
//版权所有(c)2012 Bubble Technology。版权所有。
//
#导入“ChanViewController.h”
#导入“AppDelegate.h”
#导入“ViewController.h”
@视图控制器的实现
@综合艺术封面;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
//--------------------------------从主页传入数据---------------
AppDelegate*artistname=(AppDelegate*)[[UIApplication sharedApplication]委托];
[artist setText:artistname.artistsnames];
//----------------------------------获取封面照片---------------------------
NSString*url=[NSString stringWithFormat:@”http://localhost/SNTestFiles/Photos/ArtistCovers/%@.jpg“,artist.text];
UIImage*ArtistCover=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[ArtistCover setImage:ArtistCover];
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
//----------------------------测试可变阵列-------------------------------
jams=[[NSMutableArray alloc]initWithObjects:“美杜莎”,“托因”,“难以置信”,“我不喜欢哈默”,“达米·杜罗”,“森雷尔”,“西丝·内内”,“贝诺伊·拉屎”,“为我跳舞”,“汤姆·福特·弗劳”,“魔法”,“吉迪·斯瓦格”,“杜普阿姨”,零];
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=[nsstringstringwithformat:kGETUrl,artist.text];
NSData*fetchjamsdata=[NSData DATA WITHCONTENTSOFURL:[NSURL URLWithString:fetchjamsurl]];
n错误*e;
json=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:fetchjamsdata选项:kNilOptions错误:&e];
NSDictionary*info=[json objectAtIndex:0];
NSLog(@“%@”,信息);
label.text=[info objectForKey:@“Artist”];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[json计数];
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回40;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier]自动释放];
}
///////////////////////--问题--\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                                                        \\
//                                                                        //
stuff=(NSDictionary*)[json objectAtIndex:indexath.row];
//                                                                        \\
//                                                                        \\
////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//-------------------------单元的测试字符串-----------------------
NSString*SongTitle=[[jams objectAtIndex:indexPath.row]objectForKey:@“Name”];
NSString*ArtistNames=[[artists objectAtIndex:indexPath.row]objectForKey:@“Artister”];
//------------------------------------------------------------------------
NSString*SongTitlejson=[stuff objectForKey:@“Name]”;
NSString*ArtistNamesjson=[stuff objectForKey:@“艺术家”];
cell.textlab.text=SongTitlejson;
cell.detailTextLabel.text=ArtistNamesjson;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.textlab.font=[UIFont fontWithName:@“Heiti TC”大小:18];
cell.detailTextLabel.font=[UIFont fontWi
// 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