Reputation: 543
This is my json data. Here it is Restaurant name coming one and line name coming 2 some times line name coming more then how to print the data in custom cell.please. help me
"currency": "$",
"state": "sale",
"total": 243.1,
"name": "a1238",
"restaurant_name": "\"Food Court\" Biergarten",
"date": "2016-10-16 07:52:07",
"table_no": null,
"so_id": 238,
"lines": [
{
"line_status": "pending",
"line_id": 2536,
"line_price": 1,
"line_qty": 1,
"line_name": "Käse"
},
{
"line_status": "pending",
"line_id": 2579,
"line_price": 7.8,
"line_qty": 2,
"line_name": "3 Musketiere (3x verschiedene Hefe 0,3l)"
},
Upvotes: 0
Views: 110
Reputation: 2446
Try like this:
First get all values from your response in NSMutableArray
what you want
@interface ViewController ()
{
NSMutableArray *restaurentsNamesArray;
NSMutableArray *linesArray;
NSMutableArray *linesCountArray;
}
After that in viewDidLoad
add values to those mutable arrays which you got from your response
- (void)viewDidLoad {
[super viewDidLoad];
restaurentsNamesArray = [[NSMutableArray alloc]init];
linesArray = [[NSMutableArray alloc]init];
linesCountArray = [[NSMutableArray alloc]init];
NSError *error;
// Do the stuff for get response from url here.
// And give that request below.
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSDictionary *main = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *result = [main valueForKey:@"result"];
NSArray *saleorders = [result valueForKey:@"saleorders"];
for (NSDictionary *dict in saleorders){
[restaurentsNamesArray addObject:[dict valueForKey:@"restaurant_name"]];
[linesArray addObject:[dict valueForKey:@"lines"]];
}
NSLog(@"%@",restaurentsNamesArray);
NSLog(@"%@",linesArray);
}
Now all you want is implement table view delegate methods like below:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [restaurentsNamesArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array;
if (linesArray.count > section){
array = [linesArray objectAtIndex:section];
}
return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]init];
NSArray *array;
array = [linesArray objectAtIndex:indexPath.section];
NSDictionary *dict = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [dict valueForKey:@"line_name"];
cell.detailTextLabel.text = [dict valueForKey:@"line_id"];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [restaurentsNamesArray objectAtIndex:section];
}
Here I'm just populating restaurant names in tableView
section header as NSString
.
If you want exactly like android what you shown above you have to implement below methods instead of -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
this method
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)];
[headerView setBackgroundColor:[UIColor darkGrayColor]];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake((headerView.frame.size.width/2)-47,-32,300,94)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor whiteColor];
tempLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
tempLabel.font = [UIFont boldSystemFontOfSize:17.0];
tempLabel.text= [restaurentsNamesArray objectAtIndex:section];
[headerView addSubview:tempLabel];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
return 35;
}
Like the same way I'm just populating lineName in cell.textLabel
If you want to do more , just create custom tableViewCell
and create layout how you want.
that's it.
Cheers.
Upvotes: 1
Reputation: 374
First of all create an array of Restaurant type an fetch the data from json and add it into an array just like that
var RestaurantArray = [Restaurant]()
for index in 0..<JSON["data"].count
{
let address = String(JSON["data"][index]["Address"])
let CityId = JSON["data"][index]["CityId"].int
let Description = String(JSON["data"][index]["Description"])
let Name = String(JSON["data"][index]["Name"])
//create an object of Restaurant type and map the data into object and then add it into array that we have created .
var RestaurantModel:Restaurant?
RestaurantModel.address = address
RestaurantModel.cityID = cityId
RestaurantModel.Description = Description
RestaurantArray.append(RestaurantModel)
}
now use this array in your tableview
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return RestaurantArray.count
}
I hope this will make a lot of sense to you
Cheers
Upvotes: 0
Reputation: 3008
Use your restaurant array for section
numberOfSectionsInTableView{}
and your "lines": [{}]
array for
numberOfRowsInSection{}
You may get better idea from code mentioned below
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number restaurant count
return [restaurantArray count];
}
and for multiple rows considering you have restaurant as object:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of lines count.
return [objRestaurant.Lines count];
}
Upvotes: 0