Very familiar with Android programming, however very new to iOS (and Objective – C).
I am calling a remote php file within my application and (I believe) successfully parsing the JSON results according to my NSLOG results. Example:
2013-01-17 14:24:30.611 JSON TESTING 4[1309:1b03] Deserialized JSON Dictionary = {
products = (
{
BF = "";
EN = "2342";
Measure = ft;
Name = "Brian";
"Name_id" = 1;
Home = "New York";
"DB_id" = 1;
},
{
BF = "";
EN = "2123";
Measure = ft;
Name = "Rex";
"Name_id" = 3;
Home = "New York";
"DB_id" = 5;
}
);
success = 1;
}
My question lies in how to populate this information into a table view. I can customize a prototype cell, but where do I go from there?
EDIT:
Here is my code for the view setup:
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return productArray.count;
NSLog(@"Number of arrays %u", productArray.count);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *productDictionary = [productArray objectAtIndex:indexPath.row];
cell.textLabel.text = [productDictionary objectForKey:@"BF"];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self launchTest];
}
and my .h file
@interface tpbaMasterViewController : UITableViewController
{
NSDictionary *lists;
NSArray *productArray;
}
- (void) launchTest;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
You access the objects in a
NSDictionaryusingobjectForKeymethods. For example, to get anNSArrayof the products in the dictionary:Now you have an array with two dictionary objects. For the various
UITableViewDataSourcemethods you can query the array. A couple of examples:For
– tableView:numberOfRowsInSection:, return the number of objects in the array:And for
tableView:cellForRowAtIndexPath::Declaring
productArrayin the .m file as shown below makes it visible within your view controller (assumesproductDictionaryis a property: