Hi I am new to programming.
I have some issues with NSDictionary and Table cells. I Tried to find good tutorials, but did not find anything that could help me. I am using JSONKit to parse json. I have an older version of xcode and only have simulator 4.3.
What i would like to do is to parse my Json into a table with cells.
The json I want to parse:
{[{"n_id":"1","name":"Green","age":"23"}]}
My main problem.
I dont get any errors, but when i run the app it just close automatically.
When i comment out //cell.textLabel.text = [studName objectAtIndex:indexPath.row]; and replace it with just cell.textLabel.text = @”Test”; The app and the output works fine.
I am also able to get the NSLog(); values
I think my error is in the cell.textLabel.text = [studName objectAtIndex:indexPath.row];
Thanks in advance
//Json2ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSData * JSONdata =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://myurl.php"]];
if([JSONdata length] == 0){
[JSONdata release];
return;
}
NSArray *students =[JSONdata objectFromJSONData];
studName = [[NSMutableArray alloc] init];
for(NSDictionary *student in students)
{
NSLog(@"Name: %@", [student objectForKey:@"name"]);
NSLog(@"Age: %@", [student objectForKey:@"age"]);
content = [student objectForKey:@"name"];
if(content)
[studName addObject:content];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (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];
}
// Configure the cell...
cell.textLabel.text = [studName objectAtIndex:indexPath.row];
return cell;
}
I am not sure about NSMutableArray.
//Json2ViewController.h
@interface Json2ViewController : UIViewController {
NSArray * list;
NSString * content;
NSMutableArray *studName;
}
Try this:
and in table delegate method cell for row at index