NSMutableArray と NSdictionary を使用して、mysql データベースから取得したデータをテーブル ビューにロードするアプリを作成しています。そして、私はNSDictionaryビットで間違いを犯しているのではないかと心配しています..
テーブルセルに正しくロードされる2つのコントロールNSMutableArraysがありますが、受信したNSDictionaryを分割しようとすると、キー「Name」と「Artist」のデータをセルにロードするとクラッシュします。バグを特定するのを手伝ってください。
受け取った辞書をログに記録し、出力が非常に無傷であるため、問題は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
> ////////////////////////////////////////////// /////////////////////////////////////////
ありがとう